Thursday 20 January 2022

Constructors & Destructors in C++

 Constructors are special member functions which are executed automatically when object of a class is created. These are special because they have same name as name of class. These are also special because they do not have any return type. 

Syntax:

class-name(parameter list)

{

...

}

Eg:

#include<iostream.h>

#include<conio.h>

class abc

{

int a,b;

public:

abc()

{

a=0;

b=0;

}

void show()

{

cout<<"a= "<<a<<endl<<"b= "<<b<<endl;

}

};

void main()

{

clrscr();

abc ob;

ob.show();

getch();

}

Source Code

Types of Constructors in C++:


Default Constructor

Parameterized Constructor

Copy Constructor

Dynamic Constructor

No comments:

Post a Comment