In this tutorial, you will learn in depth about C++ constructors and its types with examples.
C++ Constructors: Introduction |
Types of Constructors |
Default Constructor |
Parameterized Constructor |
Copy Constructor |
C++ constructors are special member functions which are created when the object is created or defined and its task is to initialize the object of its class. It is called constructor because it constructs the values of data members of the class.
A constructor has the same name as the class and it doesn’t have any return type. It is invoked whenever an object of its associated class is created.
When a class is instantiated, even if we don’t declare a constructor, compiler automatically creates one for the program. This compiler created constructor is called default constructor.
A constructor is defined as following
/*.....class with constructor..........*/ class class_name { ......... public: class_name(); //constructor declared or constructor prototype ......... }; class_name :: class_name() //constructor defined { //constructor function body }
If no constructor is defined in the class then the compiler automatically creates one for the program. This constructor which is created by the compiler when there is no user defined constructor and which doesn’t take any parameters is called default constructor.
/*.....format of default constructor..........*/ class class_name { ......... public: class_name() { }; //default constructor ......... };
To put it up simply, the constructor that can take arguments are called parameterized constructor.
In practical programs, we often need to initialize the various data elements of the different object with different values when they are created. This can be achieved by passing the arguments to the constructor functions when the object is created.
[adsense1]
/*.....A program to find area of rectangle .......... */
#include<iostream>
using namespace std;
class ABC
{
private:
int length,breadth,x;
public:
ABC (int a,int b) //parameterized constructor to initialize l and b
{
length = a;
breadth = b;
}
int area( ) //function to find area
{
x = length * breadth;
return x;
}
void display( ) //function to display the area
{
cout << "Area = " << x << endl;
}
};
int main()
{
ABC c(2,4); //initializing the data members of object 'c' implicitly
c.area();
c.display();
ABC c1= ABC(4,4); // initializing the data members of object 'c' explicitly
c1.area();
c1.display();
return 0;
} //end of program
Output
Area = 8 Area = 16
Note: Remember that constructor is always defined and declared in public section of the class and we can’t refer to their addresses.
Generally in a constructor the object of its own class can’t be passed as a value parameter. But the classes own object can be passed as a reference parameter.Such constructor having reference to the object of its own class is known as copy constructor.
Moreover, it creates a new object as a copy of an existing object.For the classes which do not have a copy constructor defined by the user, compiler itself creates a copy constructor for each class known as default copy constructor.
/*.....A program to highlight the concept of copy constructor.......... */
#include<iostream>
using namespace std;
class example
{
private:
int x;
public:
example (int a) //parameterized constructor to initialize l and b
{
x = a;
}
example( example &b) //copy constructor with reference object argument
{
x = b.x;
}
int display( ) //function to display
{
return x;
}
};
int main()
{
example c1(2); //initializing the data members of object 'c' implicitly
example c2(c1); //copy constructor called
example c3 = c1;
example c4 = c2;
cout << "example c1 = " << c1.display() << endl;
cout << "example c2 = " << c2.display() << endl;
cout << "example c3 = " << c3.display() << endl;
cout << "example c4 = " << c4.display() << endl;
return 0;
} //end of program
Output
example c1 = 2 example c2 = 2 example c3 = 2 example c4 = 2
Note: In copy constructor passing an argument by value is not possible.