C++ Constructor and Destructor

Describe constructors and their properties. Give example.

Constructors are special functions which have same name as class name. Whenever an object is created, class constructor is called automatically. Constructors are used to initialize the data members of the class.

Constructors are of three types:
  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
Some of the characteristics of Constructor are as follows:
  • Constructors have the same name as the class name.
  • Constructor should be declared in the public section.
  • Constructor invoked automatically when object is created.
  • Constructors do not have return type not even void.
  • Constructor cannot be virtual.
  • We cannot use constructor in union.
  • Constructors cannot be inherited.
  • There is no static constructor in cpp.
A constructor that takes no parameters is called a Default Constructor and those that take parameters are called as Parameterized Constructor.

Example:
class Area
{
     int height, width;
public:
     Area() -------------------------- Default Constructor.
     {
          height = 0;
          width = 0;
          cout<<"Default constructor called"<<endl;
     }
     Area(int h, int w) ----------------------- Parameterized Constructor
     {
          height = h;
          width = w;
          cout<<"Parameterized constructor called"<<endl;
          cout<<"Area =: "<<height*width<<endl;
     }
};
void main()
{
     int h,w;
     cout<<"Enter the height and width"<<endl;
     cin>>h>>w;
     Area obj1;
     Area obj2(h,w);
}

How constructors are executed in multilevel inheritance? Describe with example.

class A
{
public:
     A ( )
     {
          cout<<"Class A constructor is called"<<endl;
     }
};
class B : public A
{
public:
     B ( )
     {
          cout<<"Class B constructor is called"<<endl;
     }
};
class C : public B
{
public:
     C ( )
     {
          cout<<"Class C constructor is called"<<endl;
     }
};
void main ( )
{
     C obj;
}

Output:

Class A constructor is called
Class B constructor is called
Class C constructor is called

Description:

In multilevel inheritance, object of derived class call the constructor of base class and executed from reverse order, from base to derive class.

Write output with explanation.

class base
{
     base ( )
     {
          cout<<"hello";
     }
};
void main ( )
{
     clrscr();
     base();
}

Description:

The above program will not compile because constructor base() is private, so this constructor cannot be access outside the class. If you declare base() constructor in public section, then there will be no problem and output will be “hello”.

Write output with explanation

class Sample
{
public:
     int *ptr;
     Sample ( )
     {
          ptr = NULL;
     }
     Sample (int i)
     {
          ptr = newint (i);
     }
     ~Sample ( )
     {
          delete ptr;
     }
     void printVal ( )
     {
          cout <<"The value"<< *ptr;
     }
};
void SomeFunc (Sample x)
{
     cout <<"say i am in someFun"<< endl;
}
int main ( )
{
     Sample S1 = 10;
     SomeFunc (S1);
     S1.printVal ( );
}

Description:
In the above program, in main function three statements are written.
  • Sample S1 = 10; It will call the parameterized constructor. Variable “i” is initialized with value 10 and its address is stored in pointer ptr.
  • SomeFunc (S1); It will call function SomeFunc(S1) and object S1 is passes as parameter. Sample x (object of sample class) will call the default constructor and when this object will go out of scope it will call destructor. Now pointer ptr will be de-allocated and it will not reference to variable “i”;
  • S1.printVal ( ); Now ptr is not referencing to variable “i”, the it will give the error NULL pointer assignment.