Constructor Overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<conio.h> | |
using namespace std; | |
class rectangle | |
{ | |
private: | |
int length; | |
int breadth; | |
public: | |
rectangle(); | |
rectangle(int ); | |
rectangle(int ,int ); | |
void area(); | |
} | |
; | |
rectangle::rectangle() | |
{ | |
length=breadth=0; | |
cout<<"Constructor with zero parameter called\n"; | |
} | |
rectangle::rectangle(int a) | |
{ | |
length=breadth=a; | |
cout<<"Constructor with one parameter called\n"; | |
} | |
rectangle::rectangle(int a,int b) | |
{ | |
length=a; | |
breadth=b; | |
cout<<"Constructor with two parameter called\n"; | |
} | |
void rectangle::area() | |
{ | |
int area=length*breadth; | |
cout<<"\nArea \t"<<area; | |
} | |
int main() | |
{ | |
rectangle r1; | |
rectangle r2(6); | |
rectangle r3(4,6); | |
r1.area(); | |
r2.area(); | |
r3.area(); | |
getch(); | |
} | |
Sample Output:
Comments
Post a Comment
Thanks for the comment and don't forget to subscribe.