AppDividend
Latest Code Tutorials

Constructor Overloading In C++ Tutorial With Example

0

Constructor Overloading in C++ is today’s topic. Before understanding constructor overloading, we first need to understand what are constructors.  Constructors are unique methods which are invoked automatically when we create an object of the class. The primary purpose of the constructors is to initialize data members of the new object.

Constructor Overloading In C++

In C++, We can have more than one constructor in the class with the same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to function overloading.

There are three types of constructors:

  1. Default constructors – A constructor with no arguments is known as default constructors.
  2. Parameterized constructors – A constructor which has parameters are known as parameterized constructors.
  3. Copy Constructor – A copy constructor is the member function which initializes an object using another object of the same class. 

Now coming to constructor overloading, It is when we have more than one constructors present in the class, but they differ by the number of arguments present. 

Properties of constructors to keep in mind while overloading a constructor:

  1. Constructors essentially need to have the same name as of class.
  2. Constructors don’t have any return type.
  3. Constructors are always declared as public.
  4. While creating the object argument must be passed according to the requirement to let the compiler know which constructor to call.

When we add more than one constructor to a class, we call it multiple constructors.

Constructor overloading is very much similar to function overloading.

Constructor overloading the changing the number of arguments in the present the parameter list of the constructor. Hence we can use constructors of the same class for different purposes.

#Application of constructors

Suppose we have to generate a batch for a particular set of students, be it 500 students are in a single batch and the variable class_no is set to be 9 for all the students as they all are in class 9. It will be very tedious work to feed class_no variable 500 times equal to 9 for all the students, that’s why we use constructors. We create an object of the class and pass the class_no variable as the argument of the constructor to set the value of class_no = 9 for all the 500 students.

#Declaration of constructor overloading

See the following code syntax.

class A
{
   A()
   {
     body;   //constructor with no parameter 
   }

   A(int a, int b)
   {
     body;    //constructor with 2 parameters
   }
};

#Examples of constructor overloading

Example 1: Write a program to overload two constructors, one with two parameters to find the area of the rectangle and the second with one parameter to find the area of the square.

#include <iostream>
using namespace std;
class Area
{
public:
  Area()
  {
    cout << "Class and object successfully created" << endl;
  }
  Area(int length, int breadth) /*Creating a constructor with 2 parameter*/
  {

    cout << "Area of the rectangle =" << length * breadth << endl;
  }
  Area(int side) /*Creating a constructor with 1 parameter*/
  {

    cout << "Area of the square = " << side * side << endl;
  }
};

int main()
{
  Area obj1;
  Area obj2(5, 6); /*Initializing the value of length=5 and breadth=6*/
  Area obj3(5);    /*Initializing the value of side of square=5*/
  return (0);
}

See the following output.

Constructor Overloading In C++

Example 2:

Write a program to change the age of two students which are initialized in the default constructor(constructor with no arguments) as 0 to 22 for the first student and 20 for the second student and show the data as output.

See the following code example.

#include <iostream>
using namespace std;
class student
{
public:
  student() /*Creating a constructor with 2 parameter*/
  {
    cout << "Class and objects successfully created and age is initialized to 0 for both the students" << endl;

    int age_1 = 0;
    int age_2 = 0;
    cout << "\nAge of student 1 after initialization= " << age_1 << endl;
    cout << "Age of student 2 after initialization= " << age_2 << endl;
  }
  student(int age_1, int age_2) /*Creating a constructor with 1 parameter*/
  {
    cout << "\nAge of 1st student after changing the initialized value = " << age_1 << endl;
    cout << "Age of 2nd student after changing the initialized value = " << age_2;
  }
};
int main()
{
  student obj1;
  student obj2(22, 20); /*Assigning the values to the age variables of the student*/
}

See the following output.

Application of constructors overloading

You can also check out Destructors in C++ article in this blog.

Finally, Constructor Overloading In C++ Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.