Constructor Overloading in CPP

Constructor overloading: When two or more than two constructors with the sameĀ name are there. but different in parameters is called constructor overloading.
#include<iostream.h>
#include<conio.h>

class demo
{
public :
demo() // without parameter
{
int a,b,c;
cout << "enter any number";
cin >> a;

cout << "enter any number";
cin >> b;

c=a+b;
cout << c << endl;
}

demo(int a,int b) //with parameter
{
int c=a+b;
cout << c << endl;
}
};

main()
{
clrscr();
int a,b;
cout << "enter any number";
cin >> a >> b;

demo d1; //constructor without parameter will be called at this line
demo d2(a,b); // constructor with parameter will be called at this line

getch();
}

Leave a Reply