Types of Constructors
A constructor can be classified into following types:
- Dummy constructor
- Default constructor
- Parameterized constructor
- Copy constructor
- Dynamic constructor
Dummy Constructor
When no constructors are declared explicitly in a C++ program, a dummy constructor is invoked which does nothing. So, data members of a class are not initialized and contains garbage values. Following program demonstrates a dummy constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; public: void get_details() { cout << "Age of student is: " << age << endl; cout << "Branch of student is: " << branch << endl; } }; int main() { Student s1; s1.get_details(); return 0; } |
Output for the above program is as follows:
1 2 3 4 | Age of student is: 4883904 Branch of student is: |
You can see that since age and branch are not initialized, they are displaying garbage values.
Default Constructor
A constructor which does not contain any parameters is called a default constructor or also called as no parameter constructor. It is used to initialize the data members to some default values. Following program demonstrates a default constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; public: Student() //Constructor { age = 20; branch = "CSE"; } void get_details() { cout << "Age of student is: " << age << endl; cout << "Branch of student is: " << branch << endl; } }; int main() { Student s1; s1.get_details(); return 0; } |
Output for the above program is as follows:
1 2 3 4 | Age of student is: 20 Branch of student is: CSE |
Parameterized Constructor
A constructor which accepts one or more parameters is called a parameterized constructor. Following program demonstrates a parameterized constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; public: Student(int a, string b) //Constructor { age = a; branch = b; } void get_details() { cout << "Age of student is: " << age << endl; cout << "Branch of student is: " << branch << endl; } }; int main() { Student s1(21, "CSE"); s1.get_details(); return 0; } |
Output for the above program is as follows:
1 2 3 4 | Age of student is: 21 Branch of student is: CSE |
Copy Constructor
A constructor which accepts an already existing object through reference to copy the data member values is called a copy constructor. As the name implies a copy constructor is used to create a copy of an already existing object. Following program demonstrates a copy constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; class Square { private: int side; public: Square(int s) { side = s; } Square(Square & s) { side = s.side; } void display() { cout << "Side of square is: " << side << endl; } }; int main() { Square s1(10); Square s2(s1); Square s3 = s2; s1.display(); s2.display(); s3.display(); return 0; } |
Output for the above program is as follows:
1 2 3 4 5 | Side of square is: 10 Side of square is: 10 Side of square is: 10 |
In the above program you can see that there are two ways for calling a copy constructor. One way is to call the copy constructor by passing the object as a parameter (in case of s2). Other way is to directly assign an existing object to a new object (in case of s3).
Note: Copying an object through assignment only works during object declaration. After declaring an object, assignment just serves its basic purpose.
Dynamic Constructor
A constructor in which the memory for data members is allocated dynamically is called a dynamic constructor. Following program demonstrates dynamic constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; int * marks; public: Student(int n) //Constructor { marks = new int[n]; cout << "Enter marks for " << n << " subjects: "; for (int i = 0; i < n; i++) cin >> marks[i]; } void display_marks(int n) { cout << "Marks are: ; for (int i = 0; i < n; i++) cout << marks[i] << " "; } }; int main() { int n; cout << "Enter no. of subjects: "; cin >> n; Student s1(n); s1.display_marks(n); return 0; } |
Input and output for the above program are as follows:
1 2 3 4 5 | Enter no. of subjects: 4 Enter marks for 4 subjects: 20 18 19 16 Marks are: 20 18 19 16 |