Table of Contents
In this tutorial, we will learn about the C++ constructor and its type with the help examples.
A constructor is a special type of member function that is called automatically when an object is created.
In C++, a constructor has the same name as that of the class and it does not have a return type. For example, It constructs the values i.e. provides data for the object which is why it is known as constructors.
class House {
public:
// create a constructor
House() {
// body of constructor
}
};
Here, the function House()
is a constructor of the class House. Notice that the constructor
- has the same name as the class,
- does not have a return type, and
- is
public
Different Types of Constructor
C++ Default Constructor
A constructor which has no argument is known as default constructor. It is invoked at the time of creating object. In the example below, House()
is a default constructor.
// C++ program to demonstrate the use of default constructor
#include <iostream>
using namespace std;
// declare a class
class House {
private:
double length;
public:
// create a constructor
House() {
// initialize private variables
length = 6.5;
cout << "Creating a house floor ." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
// create an object
House floor;
return 0;
}
Output
Creating a house floor Length = 6.5
Here, when the floor object is created, the House()
constructor is called. This sets the length variable of the object to 6.5
.
Note: If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.
C++ Parameterized Constructor
In C++, a constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects. This is the preferred method to initialize member data.
In the example below, House(double len, double hgt)
is a parameterized constructor.
// C++ program to calculate the area of a wall
#include <iostream>
using namespace std;
// declare a class
class House {
private:
double length;
double height;
public:
// create parameterized constructor
House(double len, double hgt) {
// initialize private variables
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
House floor(10.5, 8.6);
House ceiling(8.5, 6.3);
cout << "Area of floor : " << floor.calculateArea() << endl;
cout << "Area of ceiling : " << ceiling.calculateArea() << endl;
return 0;
}
Output
Area of floor: 90.3 Area of ceiling: 53.55
Here, we have created a parameterized constructor House(double len, double hgt)
that has 2 parameters: double len
and double hgt
. The values contained in these parameters are used to initialize the member variables length and height.
When we create an object of the House class, we pass the values for the member variables as arguments. The code for this is:
House floor(10.5, 8.6);
House ceiling(8.5, 6.3);
With the member variables thus initialized, we can now calculate the area of the floor and ceiling with the calculateArea()
function.
C++ Copy Constructor
The copy constructor in C++ is used to copy data of one object to another.
#include <iostream>
using namespace std;
// declare a class
class House{
private:
double length;
double height;
public:
// parameterized constructor
House(double len, double hgt) {
// initialize private variables
length = len;
height = hgt;
}
// copy constructor with a House object as parameter
House(House &values){
// initialize private variables
length = values.length;
height = values.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of House class
House floor(10.5, 8.6);
// print area of floor
cout << "Area of floor"<< wall1.calculateArea() << endl;
// copy contents of floor to another object ceiling
House ceiling=floor;
// print area of ceiling
cout << "Area of ceiling:" << ceiling.calculate.Area()<<endl;
return 0;
}
Output
Area of floor: 90.3 Area of ceiling:90.3
In this program, we have used a copy constructor to copy the contents of one object of the House class to another. The code of the copy constructor is:
House(House &values){
length = values.length;
height = values.height;
}
Notice that the parameter of this constructor has the address of an object of the House class.
We then assign the values of the variables of the first object to the corresponding variables of the second object. This is how the contents of the object are copied.
In main()
, we then create two objects floor and ceiling and then copy the contents of the first object to the second with the code
House ceiling=floor;
Note: A constructor is primarily used to initialize objects. They are also used to run a default code when an object is created.