C++ Constructor

In C++, Constructor is used to initialize the variables of the class and to allocate memory. Constructor is similar to functions but with some differences.

Characteristics of Constructor:

  1. Constructor is called automatically when an object is created.
  2. Constructor name is same as class name.
  3. Constructor should be declared in public section.
  4. Constructor do not have return type, no even void.
  5. Constructor can be overloaded like function overloading.
  6. Constructor can have default arguments.

Default Constructor

In every C++ program there is a constructor known as default constructor. We can also create a default constructor which is parameterless.

Example 1: Default constructor. 1380

class example
{
    public: 
    example()
    {
       cout<<"Default Constructor Called";
    }
};

void main()
{
    clrscr();
    example ex; // default constructor called
    getch();
}
Default Constructor Called

Parameterized Constructor

In constructor, we can pass parameters same as we did in functions.

Example 2: WAP to find Sum of 2 numbers using parameterized constructor. 293

class sum
{
    int s;
    public: 
    sum(int a, int b)
    {
        s=a+b;
        cout<<"Sum = "<<s;
    }
};

void main()
{
    int a,b;
    clrscr();
    cout<<"Enter 1st Number: ";
    cin>>a;
    cout<<"Enter 2nd Number: ";
    cin>>b;
    sum s(a, b); // constructor called with 2 parameters
    getch();
}
Enter 1st Number: 10
Enter 2nd Number: 15
Sum = 25
Advertisement

Constructor Overloading

In Constructor overloading, we can create multiple constructors that have same name but different parameters.

Example 3: WAP to calculate Area of Circle & Rectangle using constructor overloading. 218

class area
{
    float a, a2;
    public:
    area(float r)
    {
        a = 3.14f * r * r;
        cout<<endl<<"Area of Circle = "<<a;
    }
    area(int l, int b)
    {
        a2 = l*b;
        cout<<"\nArea of Rectangle = "<<a2;
    }
};

void main()
{
    float r;
    int l, b;
    clrscr();
    cout<<"Enter Radius: ";
    cin>>r;
    cout<<"Enter length: ";
    cout<<"Enter breadth: ";
    cin>>l;
    cin>>b;
    area x(r);
    area y(l,b);
    getch();
}
Enter Radius: 5.5
Enter length: 10
Enter breadth: 20
Area of Circle = 94.985
Area of Rectangle = 200
Advertisement

Constructor with default argument

In parameterized constructor we have to match its arguments, but in default argument we can omit the argument value, In that case constructor takes default value of that variable assigned in the body of the constructor. There can be one or more default arguments in a constructor.

Example 4: WAP to find Simple Interest using constructor with default argument. 355

class si
{
    float s;
    public:
    si(float p, float t, float r=10.5f)
    {
        s = p*r*t/100;
        cout<<endl<<"Simple Interest = "<<s;
    }
};

void main()
{
    float p, r, t;
    clrscr();
    cout<<"Enter Principle: ";
    cout<<"Enter Rate: ";
    cout<<"Enter Time: ";
    cin>>p;
    cin>>r;
    cin>>t;
    si obj(p,t,r);
    si obj2(p,t);
    getch();
}
Enter Principle: 1000
Enter Rate: 10.5
Enter Time: 2
Simple Interest = 210
Advertisement

Copy Constructor

Copy constructor is used to copy values of one object into another object.

Example 5: Copy Constructor. 280

class copy
{
    int a;
    public:
    copy() //default constructor
    {
        a=10;
    }
    copy(copy &obj1) //copy constructor
    {
	    a = obj1.a;
    }
    void output()
    {
        cout<<endl<<"Value of A = "<<a;	
    }
};
void main()
{
    clrscr();
    copy obj1; //constructor called
    copy obj2 = obj1; //copy constructor called
    obj1.output();
    obj2.output();
    getch();
}
Value of A = 10
Value of A = 10
Advertisement

C++ Destructor

C++ Destructor is used to destroy the objects that have been created by a constructor, when they are no longer required. Destructor is used to clean up the memory storage.

Note: new keyword is used to allocate memory, and delete keyword is used to free that memory.

Rules to declare a Destructor

  1. Destructor name is the same as class name but is preceded by a tilde (~).
  2. Destructor never takes any argument.
  3. Destructor called automatically when the user exit the program or block or function.

Example 6: Destructor. 292

class example
{
   public:
   example
   {
      cout<<"Constructor Called";
   }
   ~example
   {
      cout<<"Destructor Called";
   }
};
void main()
{
   clrscr();
   example obj;
   {
      example obj2;
   }
   {
      example obj3;
   }
   getch();
}
Constructor Called
Constructor Called
Destructor Called
Constructor Called
Destructor Called
Destructor Called
Advertisement