Overloading Of Constructor In C++ | Constructor Overloading Explained With Real Life Examples

 Overloading Of Constructor In C++ 



What is constructor overloading?

As you know what is a constructor and also what is function overloading. So you know constructors are special class methods which is basically a function which runs at the starting of class always.

So as you know how to overload a function then the constructor is also a function so you can overload a constructor as same as function overloading.


#include <iostream>
using namespace std;


class calculator
{

    public:
    calculator(int a, int b) //constructor with 2 int type parameter
    {
        cout<<"I AM CONSTRUCTOR "<<a+b<<endl;

    }

    calculator(int a, int b, int c) //constructor with 3 int tpe parameter
    {
        cout<<"I AM A OVERLOADED CONSTRUCTOR "<<a+b+c<<endl;
    }

    
};
int main()
{
    calculator c1(1,2);
    calculator c2(1,2,4);
    return 0;
}


Output - 

I AM CONSTRUCTOR 3

I AM A OVERLOADED CONSTRUCTOR 7




Comments

Popular posts from this blog

Constructor In C++ | Constructor Explained With Real Life Example

Destructor In C++ | Destructor Explained With Real Life Examples

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example