Types of Constructor in C++

Types of Constructor in C++

Types of Constructor in C++

 

There are three types of Constructor :
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor

 

DEFAULT CONSTRUCTOR :

A constructor that has zero parameter list or in other word , a constructor that accept no argument is called zero argument constructor or default constructor .

SYNTAX :

class_name ()

{

// body of constructor

}

 

Code :

// C++ program to demonstrate the use of default constructor

#include <iostream>
using namespace std;

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

  public:
    // default constructor to initialize variable
    Wall()
      : length{5.5} {
      cout << "Creating a wall." << endl;
      cout << "Length = " << length << endl;
    }
};

int main() {
  Wall wall1;
  return 0;
}


 Output :
Creating a Wall
Length = 5.5

PARAMETERIZED CONSTURCTOR :

The constructor that can take arguments are called parameterized constructor .

Using parameterized constructor we can intialize the various data elements of different objects with different values when they are created .

SYNTAX :

class_name (parameters…..)[

// body of constructor

}

 

CODE :

// C++ program to calculate the area of a wall

#include <iostream>
using namespace std;

// declare a class
class Wall {
  private:
    double length;
    double height;

  public:
    // parameterized constructor to initialize variables
    Wall(double len, double hgt)
      : length{len}
      , height{hgt} {
    }

    double calculateArea() {
      return length * height;
    }
};   int main() {
  // create object and initialize data members
  Wall wall1(10.5, 8.6);
  Wall wall2(8.5, 6.3);

  cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
  cout << "Area of Wall 2: " << wall2.calculateArea();

  return 0;
}


OUTPUT :
Area of Wall 1: 90.3                                                        Area of Wall 2: 53.55

 

 

COPY CONSTRUCTOR :

A copy constructor is a member function that initializes an object using another object of the same class .

In other words, a constructor which creates an object by initialize it with an object of the same class , known as copy constructor .

SYNTAX :

class_name ( class_name & obj ) {

// body of constructor

}

 

CODE :

#include <iostream>
using namespace std;

// declare a class
class Wall {
  private:
    double length;
    double height;

  public:

    // initialize variables with parameterized constructor
    Wall(double len, double hgt)
      : length{len}
      , height{hgt} {
    }

    // copy constructor with a Wall object as parameter
    // copies data of the obj parameter
    Wall(const Wall& obj)
      : length{obj.length}
      , height{obj.height} {
    }
   double calculateArea() {
         return length * height;
     }
};

int main() {
  // create an object of Wall class
  Wall wall1(10.5, 8.6);

  // copy contents of wall1 to wall2
  Wall wall2 = wall1;

  // print areas of wall1 and wall2
  cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
  cout << "Area of Wall 2: " << wall2.calculateArea();

  return 0;
}



OUTPUT :
Area of Wall 1: 90.3
Area of Wall 2: 90.3

 

Topics Covered

  • constructors
  • c++ constructors
  • default constructors
  • what is constructor in programming
  • understanding constructors
  • parameterized constructors
  • copy constructor
  • types of constructor in c++

 


 

 


 

Thanks for reading this blog. Hope you get satisfied with the blog and definitely this blog must have valued your time and effort of reading.

Take a time to connect our other digital creations such as Instagram , Facebook and Youtube.

 

🧑‍💻🧑‍💻 Social Media Links of Tech DCode :

👉🏻 YouTube : https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
👉🏻 Instagram : https://www.instagram.com/thetechdcode/
👉🏻 Facebook Page : https://www.facebook.com/thetechdcode
👉🏻 Twitter : https://twitter.com/thetechdcode
👉🏻 Telegram Channel : https://t.me/thetechdcode
👉🏻 Tech DCode Linktree : https://linktr.ee/thetechdcode
👉🏻 My Personal Handles : https://linktr.ee/virtualshivamin

🧑‍💻🧑‍💻 Social Media Links of SHIVAM SINGH (OWNER) :

👉🏻 Instagram : https://www.instagram.com/virtualshivamin/
👉🏻 Facebook Page : https://www.facebook.com/virtualshivamin/
👉🏻 Twitter : https://twitter.com/virtualshivamin/
👉🏻 Personal Linktree : https://linktr.ee/virtualshivamin 

 

Leave a Reply

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