A Constructor is a special member function of a class. It initializes the memory object data by default when we create an object of the class. Constructor name is same as the class name.
Its name is constructor because it constructs the values of data members of the class.
There are three types of constructor:
1.Default Constructor: The constructor with no arguments is called the default constructor.
2. Parameterized Constructor: The constructor which has some arguments is known as the parameterized constructor.
3. Copy Constructor: The constructor which takes the reference to its own class as an argument is known as copy constructor.
#include<iostream.h> #include<conio.h> #include<string.h> class A { int rno; char name[10]; public: A() //default constructor { rno=100; strcpy(name,"tanmay"); } A(int n,char str[]) //parameterized constructor { rno=n; strcpy(name,str); } A(A &t1) //copy constructor { rno=t1.rno; strcpy(name,t1.name); } void show() { cout << rno << "\t" << name << endl; } }; void main() { A a1,a2(201,"Ayushi"),a3(a2); clrscr(); a1.show(); a2.show(); a3.show(); getch(); } Output: 100 tanmay 201 ayushi 201 ayushi