C++ constructor is a special member function that is invoked automatically, whenever an instance of the class to which it belongs is created. The execution of such a function guarantees that the instance variables of the class will be initialized properly.
C++ constructor function is unique from all other functions in a class because it is not called using some instance of the class, but is invoked whenever we create an object of the class. It is called constructor as it is used to construct the data members of the class.
A constructor is declared and defined bellow:
// data members
public: class_name(argument_list); // constructor declaration
// other member function
class_name::class_name(argument_list); // constructor definition
// initialize the data members
C++ constructor’s characteristics
As seen from the above format, the constructors should have the following characteristics:
- They should be declared in the public section.
- Unlike other member functions, they should have same name as that of the class.
- They do not have any return types and therefore, cannot return values.
- They cannot be inherited, through a derived class can call the base class constructor.
- They can have default argument.
- An object with a constructor (or destructor) cannot be used as a member of a union.
- We cannot refer to their addresses.
- They make implicit calls to the operators new and delete when memory allocation is required. When a constructor is declared for a class, initialization of the class objects become mandatory.
C++ constructor example
CRectangle(int, int); //constructor declaration
int area (void);
CRectangle:: CRectangle(int m, int n) // constructor definition
CRectangle rec(4,5); // constructure called
cout<<”Ares is” <<rec.area();
In the case of parameterized constructors the initial values must be passed as arguments to the constructor function, when an object is declared. This can be done in two ways:
- By calling the constructor explicitly
- By calling the constructor implicitly
CRectangle rect= CRectangle(5,6); //explicit call
This statement creates an object rect and passes the arguments 5 and 6 to the parameterized constructor CRectangle. The second method is implemented as follows:
CRectangle rect(5, 6); //implicit call
This method is known as shorthand method, which is used very often and is easy to implement.
C++ Copy constructor
Initialization takes place only once when the object is created, whereas assignment can occur whenever desired in the program. Therefore, they differ conceptually. For e.g.,
String s1(“this is a string”);
String s2(s1); //Initialization
The first statement declares an object s1 and passes a string as an argument to its constructor. The data members of s1 are initialized using the String. The second statement declares another object s2 and contains an object as its argument. Since there is no constructor having an object as its formal argument, the compiler itself initializes the second object’s data members with those of the formal object. Since one of the members is a pointer, the problem of assignment arises here. Defining a constructor function that takes object as its argument solves the problem of initialization. This C++ constructor is also called “copy constructor”. It is called in three contexts:
- When one object of a class is initialized to another of the same class.
- When an object is passed by value as an argument to a function.
- When a function returns an object.
String(const String &s2)
cptr = new char[size+1];
Copy constructor can be invoked as
String s(10, “hello”);
String s2(s1) or String s2=s1;