This is a sample C++ program on Constructor Overloading. Using Constructor Overloading, here we calculate area of rectangles with default values.
#include <iostream.h>
#include <conio.h>
class rectangle
{
private:
int length, breadth;
public:
rectangle ( )
{
length = breadth = 0;
cout<<"Constructor With Zero Parameter Called.\n";
}
rectangle (int a)
{
length = breadth = a;
cout<<"Constructor With One Parameter Called. \n";
}
rectangle (int a, int b)
{
length = a; breadth = b;
cout<<"Constructor With Two Parameters Called. \n";
}
int area ( )
{
return (length*breadth);
}
};
int main ( )
{
clrscr ( );
rectangle r1;
rectangle r2 (10);
rectangle r3 (10, 15);
cout<<"\n Area Of First Rectangle = "<<r1.area ( );
cout<<"\n Area Of Square = "<<r2.area ( );
cout<<"\n Area Of Second Rectangle = "<<r3.area ( );
getch();
return 0;
}
OUTPUT:
Constructor With Zero Parameter Called.
Constructor With One Parameter Called.
Constructor With Two Parameters Called.
Area Of First Rectangle = 0
Area Of Square = 100
Area Of Second Rectangle = 150
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com