
Constructors in C++
A constructor is a special member function of a class that is used to initialize the new objects in a class. A constructor has almost the same properties as a class.
Constructors are of three types:
- Non-parametrized constructor
- parametrized constructor
- copy constructor
Topics Covered
- Constructors in C++
- Write a C++ program to demonstrate an example of Default Constructor or No argument or Non-parametrized constructor
- C++ program to demonstrate an example of Parameterized Constructor
- Write a C++ program to demonstrate an example of Copy Constructor
1. Write a C++ program to demonstrate an example of Default Constructor or No argument or Non-parametrized constructor
#include <iostream>
using namespace std;
//declaration of the class
class Demo
{
//Private block to declare data member( X,Y ) of integer type.
private:
int X;
int Y;
//Public block of member function to access data members.
public:
/*Declaration of default or no argument constructor to initialize data members. */
Demo ();
//To take input from user.
void Input();
//To display output onn screen.
void Display();
};
//End of class
//Definition of the constructor.
Demo:: Demo()
{
X = 0;
Y = 0;
}
//Definition of the Input() member function.
void Demo:: Input()
{
cout << "Enter Value of X: "; cin >> X;
cout << "Enter Value of Y: "; cin >> Y;
}
//Definition of the Display() member function.
void Demo:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
Demo d ;
//Display value of the data member.
cout << endl <<"Method 1: " << endl;
cout << "Value after initialization : " ;
d.Display();
d.Input();
cout << "Value after User Input : ";
d.Display();
//We can also create object like this
Demo d1 = Demo();
//Display value of data member.
cout << endl << "Method 2: " << endl;
cout << "Value after initialization : ";
d1.Display();
return 0;
}
OUTPUT:
2. Write a C++ program to demonstrate an example of Parameterized Constructor
#include <iostream>
using namespace std;
//declaration of the class
class Demo
{
//Private block to declare data member( X,Y ) of integer type.
private:
int X;
int Y;
//Public block of member function to access data members.
public:
//Declaration of perameterized constructor to initailize data members.
Demo (int a, int b);
//To take input
void Input();
//To display output of the code.
void Display();
};
//End of class
//Definition of the constructor.
Demo:: Demo(int a, int b)
{
X = a;
Y = b;
}
//Definition of the Input() member function.
void Demo:: Input()
{
cout << "Enter Value of X: "; cin >> X;
cout << "Enter Value of Y: "; cin >> Y;
}
//Definition of the Display() member function.
void Demo:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
Demo d(10,20) ;
//Display value of data member.
cout << endl <<"Method 1: " << endl;
cout << "Value after initialization: " ;
d.Display();
d.Input();
cout << "Value after User Input : ";
d.Display();
//another way to create objects
Demo d1 = Demo(10,20);
//Display value of the data member.
cout << endl << "Method 2: " << endl;
cout << "Value after initialization: ";
d1.Display();
return 0;
}
OUTPUT:
3. Write a C++ program to demonstrate an example of Copy Constructor
#include <iostream>
using namespace std;
// Declaration of the class
class Demo
{
// declare data member( X,Y ) of integer type as a private
private:
int X;
int Y;
//Public block of member function to access data members.
public:
//Declaration of parameterized constructor to initialize data members.
Demo (int a, int b);
//Declaration of copy constructor to initialize data members.
Demo (const Demo &d);
//To display output on screen.
void Display();
};
//End of the class
//Definition of the parameterized constructor
Demo:: Demo(int a, int b)
{
X = a;
Y = b;
}
//Definition of the copy constructor.
Demo:: Demo(const Demo &d)
{
X = d.X;
Y = d.Y;
}
//Definition of the display () member function.
void Demo:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
Demo d1(10,20) ;
//Display the value of the data member.
cout << endl <<"D1 Object: " << endl;
cout << "Value after initialization : " ;
d1.Display();
//Intialize object with other object using the copy constructor
Demo d2 = Demo(d1);//also write like this :Demo d2(d1);
//Display value of the data member.
cout << endl << "D2 Object: " << endl;
cout << "Value after the initialization : ";
d2.Display();
return 0;
}
OUTPUT: