C plus plus Constructor

C++ Constructor

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:
Class class_name
{
// 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.
For e.g. the following program segment that illustrates the use of C++ constructor:
  • 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

class CRectangle
{
int x,y;
public:
CRectangle(int, int); //constructor declaration
int area (void);
};
CRectangle:: CRectangle(int m, int n) // constructor definition
{
x=m;
y=n;
}
main()
{
CRectangle rec(4,5);  // constructure called
cout<<”Ares is” <<rec.area();
}
Here, C++ constructor CRectangle() is called when the object rec is created. Constructors may or may not have any arguments. The constructors that can take arguments are called parameterized constructors. In the previous example constructors CRectangle is a parameterized constructor as it takes two arguments m and n.
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
The following declaration the first method:
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.
All these situations result in a copy of one object to another. Hence, a copy constructor is very important if the class contains pointer as data member.
A C++ copy constructor for the above example is given below:
String(const String &s2)
{
size s2.size;
cptr = new char[size+1];
strcpy(cptr,s2.cptr);
}
Copy constructor can be invoked as
String s(10, “hello”);
String s2(s1) or String s2=s1;
It is just like a normal C++ constructor but the argument here is an object of the class. The existing instance should never be modified by the copy constructor function, so the keyword const should be used in the function signature. The process of initializing the copy constructor is known as copy initialization.

Leave a Reply

Your email address will not be published.