Constructor and Destructor in C++.

Constructor

  • Constructor is a member function that has the same name as its class and is used with a valid initial value to initialize the objects of that class type. 
  • When constructing an object, Constructor is automatically called.
www.mskuthar.blogspot.com
Constructor

Types of Constructor

Default Constructor-: 
  • A constructor that accepts no parameters is known as default constructor. 
  • If no constructor is defined then the compiler supplies a default constructor.

Circle :: Circle()
{
    radius = 0;
}

Parameterized Constructor -: 
  • A constructor that receives arguments/parameters, is called parameterized constructor.

Circle :: Circle(double r)
{
    radius = r; 
}
Copy Constructor-: 
  • A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates the copy of the passed object.

Circle :: Circle(Circle &t)
{
    radius = t.radius; 
}
  • There can be multiple constructors of the same class, provided they have different signatures.

//Program to calculate the area of a rectangle and display it.

#include <iostream.h>
#include <conio.h>
class Area
{
    private:
       int length;
       int breadth;
    public:
       // Constructor
       Area(): length(5), breadth(2){ }
       void GetLength()
       {
           cout << "Enter length and breadth respectively: ";
           cin >> length >> breadth;
       }
       int AreaCalculation() {  return (length * breadth);  }
       void DisplayArea(int temp)
       {
           cout << "Area: " << temp;
       }
};
int main()
{
    Area A1, A2;
    int temp;
    A1.GetLength();
    temp = A1.AreaCalculation();
    A1.DisplayArea(temp);
    cout << endl << "Default Area when value is not taken from user" << endl;
    temp = A2.AreaCalculation();
    A2.DisplayArea(temp);
    return 0;
}
Note:- In this program, class Area is created to handle area related functionalities. It has two data members length and breadth.

Expected Output:-
Enter length and breadth respectively: 6
7
Area: 42
Default Area when value is not taken from user
Area: 10

Constructor Overloading
  • Constructor can be overloaded in a similar way as function overloading.
  • Overloaded constructors have the same name (name of the class) but different number of arguments.
  • Depending upon the number and type of arguments passed, specific constructor is called.
  • Since, there are multiple constructors present, argument to the constructor should also be passed while creating an object.
// Source Code to demonstrate the working of overloaded constructors
#include <iostream.h>
#include <conio.h>
class Area
{
    private:
       int length;
       int breadth;
    public:
       // Constructor with no arguments
       Area(): length(5), breadth(2) { }
       // Constructor with two arguments
       Area(int l, int b): length(l), breadth(b){ }
       void GetLength()
       {
           cout << "Enter length and breadth respectively: ";
           cin >> length >> breadth;
       }
       int AreaCalculation() {  return length * breadth;  }
       void DisplayArea(int temp)
       {
           cout << "Area: " << temp << endl;
       }
};
int main()
{
    Area A1, A2(2, 1);
    int temp;
    cout << "Default Area when no argument is passed." << endl;
    temp = A1.AreaCalculation();
    A1.DisplayArea(temp);
    cout << "Area when (2,1) is passed as argument." << endl;
    temp = A2.AreaCalculation();
    A2.DisplayArea(temp);
    return 0;
}
Expected Output:-
Default Area when no argument is passed.
Area: 10
Area when (2,1) is passed as argument.
Area: 2


Destructor
  • A destructor is a member function with a stable name as the one of its class followed by a ~(tilde) sign and used to destroy the objects that a constructor generated. 
  • When the scope of an object is over, it is invoked.\
~Circle() {}
Example : 
  • In the following program constructors, destructor and other member functions are defined inside class definitions. 
  • Since we are using multiple constructor in class so this example also illustrates the concept of constructor overloading.
#include<iostream.h>
#include<conio.h>
class Circle //specify a class
{
    private :
        double radius; //class data members
    public:
        Circle() //default constructor
        {
            radius = 0;
        }
        Circle(double r) //parameterized constructor
        {
            radius = r;
        }
        Circle(Circle &t) //copy constructor
        {
            radius = t.radius;
        }
        void setRadius(double r) //function to set data
        {
            radius = r;
        }
        double getArea()
        {
            return 3.14 * radius * radius;
        }
        ~Circle() //destructor
        {} 
};

int main()
{
    Circle c1; //defalut constructor invoked
    Circle c2(2.5); //parmeterized constructor invoked
    Circle c3(c2); //copy constructor invoked
    cout << c1.getArea()<<endl;
    cout << c2.getArea()<<endl;
    cout << c3.getArea()<<endl;
    return 0;
}

Another way of Member initialization in constructors
  • The constructor for this class could be defined as.

Circle :: Circle(double r)
{
    radius = r; 
}
It could also be defined using member initialization as:

Circle :: Circle(double r) : radius(r)

Post a Comment

Previous Post Next Post