write a program in C++ to Create a rectangle class having constructor to initialize square and rectangle using constructor overloading - WAY2IT

Latest Post :

write a program in C++ to Create a rectangle class having constructor to initialize square and rectangle using constructor overloading



Code :-

#include<iostream.h>
#include<conio.h>
#include<math.h>
class rectangle
{
public:
float l,b;
//constructor for square
rectangle(int x)
{
l=x;
b=x;
}
//constructor for normal rectangle

rectangle(int x,int y)
{
l=x;
b=y;
}
void length()
{
cout<<endl<<"l="<<l;
}
void breadth()
{
cout<<endl<<"b="<<b;
}
float area()
{
return(l*b);
}
float perimeter()
{
return(2*(l+b));
}
float diagonal()
{
return sqrt((l*l)+(b*b));
}
};
void main()
{
rectangle rect(5,4);
rectangle square(5);
clrscr();
cout<<"\n\n========RENTANGLE========"<<endl;
rect.length();
rect.breadth();
cout<<endl<<"diagonal="<<rect.diagonal()<<endl;
cout<<"Area of rectangle="<<rect.area()<<endl;
cout<<"perimeter of rectangle="<<rect.perimeter();
cout<<"\n\n========SQUARE========"<<endl;
square.length();
square.breadth();
cout<<endl<<"diagonal square="<<square.diagonal()<<endl;

cout<<"Area of square="<<square.area()<<endl;
cout<<"perimeter of square="<<square.perimeter();
getch();
}



Output :-