Constructor in C++

Constructor in C++

Constructor in C++ is a special member function of a class that is executed whenever we create a new object of that class. A contractor will have exact same as the class and it does not have any return type at all, not even void. A constructor can be very useful for setting initial values for variables.

 

Example of constructor in C++

class  House {
  public:
    // create a constructor
    House() {
      // code
    }
};

 

Types of Constructors

    • Default constructor

    • Parameterized constructor

    • Copy constructor

1. Default Constructor

A default constructor is a constructor that does not take any argument. This constructor has no parameter

Program of default constructor

#include <iostream>
using namespace std;

// declare a class
class  Box {
  private:
    double length;

  public:
    Box() {
      length = 3.5;
      cout << "Box." << endl;
      cout << "Length = " << length << endl;
    }
};

int main() {
  Box box1;
  return 0;
}

Output

Box Length = 3.5

A default constructor is so important for the initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.

 

2. Parameterized constructor

Parameterized constructors are the constructor having parameters. Using this constructor you can provide different values to data members of different objects, bypassing appropriate values as argument

Program of Parameterized constructor

#include <iostream>
using namespace std;

// declare a class
class Box {
  private:
    double length;
    double breadth;

  public:
    Box(double len, double bre) {
      length = len;
      breadth = bre;
    }

    double calculateArea() {
      return length * breadth;
    }
};

int main() {
  Box box1(10, 5.5);
  Box box2(8.7, 4.2);

  cout << "Area of Box 1: " << bax1.calculateArea() << endl;
  cout << "Area of Box 2: " << box2.calculateArea();

  return 0;
}

Output

Area of Box 1: 55.0
Area of Box 2: 36.54

 

3. Copy Constructor

The copy constructor is a constructor which creates an object by initializing it with an objectof the same class, which.

Copy constructor used to:

  • Initializing one object from another of the same type
  • Copy an object to pass it as an argument to a function.
  • Copy an object to return it from a function

 

Program of Copy constructor in C++

#include <iostream>
using namespace std;

// declare a class
class Box {
  private:
    double length;
    double breadth;

  public:

    // initialize variables with parameterized constructor
    Box(double len, double bre) {
      length = len;
      breadth = bre;
    }

    // copy constructor with a Wall object as parameter
    // copies data of the obj parameter
    Box(Box &obj) {
      length = obj.length;
      breadth = obj.breadth;
    }

    double calculateArea() {
      return length * breadth;
    }
};

int main() {
  // create an object of Box class
  Box box1(10.5, 8.6);

  // copy contents of box1 to box2
  Box box2 = box1;

  // print areas of box1 and box2
  cout << "Area of Box 1: " << box1.calculateArea() << endl;
  cout << "Area of Box 2: " << box2.calculateArea();

  return 0;
}

Output

Area of Box 1: 90.3
Area of Box 2: 90.3

 

Read more, Class and Object in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *