C++ Constructors

194 views

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,

class House { public: // create a constructor House() { // body of constructor } };
Code language: C++ (cpp)

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 with no parameters is known as a default constructor. In the example above, House() is a default constructor.

Example : C++ 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; }
Code language: C++ (cpp)

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 with parameters is known as a parameterized constructor. This is the preferred method to initialize member data.

Example : C++ 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; }
Code language: C++ (cpp)

Output

Area of floor: 90.3
Area of ceiling: 53.55

Here, we have created a parameterized constructor House() 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);
Code language: C++ (cpp)

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.

Example : C++ Copy Constructor

#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 &obj){ // initialize private variables length = obj.length; height = obj.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; }
Code language: C++ (cpp)

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 &obj){ length = obj.length; height = obj.height; }
Code language: C++ (cpp)

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;
Code language: C++ (cpp)

Note: A constructor is primarily used to initialize objects. They are also used to run a default code when an object is created.

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks