Multiple Constructor in a class(Constructor Overloading) in c++
Multiple Constructor in a class(Constructor Overloading)
-In a single class there may be more than one constructorThis situation is referred as overloaded constructor or we can say construction is overloaded
#include<iostream>
#include<conio.h>
using namespace std;
class rectangle
{
int length, breadth;
public:
rectangle()
{
length=0;
breadth=0;
}
rectangle(int a, int b)
{
length=a;
breadth=b;
}
rectangle(int x)
{
length=x;
breadth=x;
}
void display()
{
cout<<"Area is:"<< (length*breadth)<<endl;;
}
};
int main()
{
rectangle r1;
rectangle r2(10);
rectangle r3(10,5);
r1.display();
r2.display();
r3.display();
getch();
}
Output:
Post a Comment