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 constructor
This 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:


No comments

Powered by Blogger.