Thursday, 20 January 2022

Types of Constructors in C++


Default Constructor

Parameterized Constructor

Copy Constructor

Dynamic Constructor

//Default Constructor By Programmer

#include<iostream.h>

#include<conio.h>

class abc

{

int a,b;

public:

abc()

{

a=10;

b=20;

}

void show()

{

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

}

};

void main()

{

clrscr();

abc ob;

ob.show();

getch();

}

The above example shows that a & b will always be initialized to 10 & 20. It is default constructor by the programmer.

If we do not provide any constructor then default constructor by the compiler will initialize a & b with garbage value.


No comments:

Post a Comment