#include
class rectangle
{
private:
int length,breadth;
public:
rectangle()
{
length=breadth=0;
cout<<"\n\nContructor with 0 Parameter is called";
}
rectangle(int a)
{
length=breadth=a;
cout<<"\n\nConstructor with 1 Parameter is called\n";
}
rectangle(int a,int b)
{
length=a;
breadth=b;
cout<<"\n\nContructor with 2 Parameters is called";
}
int area()
{
return(length*breadth);
}
};
int main()
{
clrscr();
rectangle r1;
rectangle r2(5);
rectangle r3(7,8);
cout<<"\n\nArea of first rectangle="<
getch();
return 0;
}
/******************************************
OUTPUT
********************************************
Contructor with 0 Parameter is called
Constructor with 1 Parameter is called
Contructor with 2 Parameters is called
Area of first rectangle=0
Area of second rectangle=25
Area of third rectangle=56
By Abhishek Wadhwa
*/
Check out some other constructor related articles here
ReplyDelete1. Constructor overloading in C++
2. Copy Constructor in C++
3. Constructor with default arguments in C++
4. Parameterized Constructor in C++