In: Computer Science
Using basic C++( without using <Rectangle.h> )
Note : Do you want the whole code in single file ...? Could u plz tell me ...so that I will divide the divide the code...thank You
/**********************************************/
#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle
{
private:
float length;
float width;
public:
Rectangle()
{
this->length = 1;
this->width = 1;
}
Rectangle(float l, float w)
{
this->length = l;
this->width = w;
}
void setLength(float l)
{
this->length = l;
}
void setWidth(float w)
{
this->width = w;
}
float perimeter()
{
return 2 * (length + width);
}
float area()
{
return length * width;
}
void show()
{
cout << "Length :" << length << endl;
cout << "Width :" << width << endl;
}
bool sameArea(Rectangle r)
{
if (area() == r.area())
return true;
else
return false;
}
};
int main()
{
// Creating instance of Rectangle class
Rectangle r1(5, 2.5), r2(7.5, 12.6);
cout << "Rectangle#1:" << endl;
r1.show();
// Displaying the area of Rectangle
cout << "Area :" << r1.area() << endl;
// Displaying the perimeter of Rectangle
cout << "Perimeter :" << r1.perimeter() <<
endl;
cout << "\nRectangle#2:" << endl;
r2.show();
// Displaying the area of Rectangle
cout << "Area :" << r2.area() << endl;
// Displaying the perimeter of Rectangle
cout << "Perimeter :" << r2.perimeter() <<
endl;
// Creating instance of Rectangle class
Rectangle r3, r4(4, 3);
cout << "Rectangle#3:" << endl;
r3.show();
// Displaying the area of Rectangle
cout << "Area :" << r3.area() << endl;
cout << "Perimeter :" << r3.perimeter() <<
endl;
cout << "\nRectangle#4:" << endl;
r4.show();
// Displaying the area of Rectangle
cout << "Area :" << r4.area() << endl;
cout << "Perimeter :" << r4.perimeter() <<
endl;
if (r3.sameArea(r4))
{
cout << "Rectangle#3 and Rectangle#4 have same area" <<
endl;
}
else
{
cout << "Rectangle#3 and Rectangle#4 have not same area"
<< endl;
}
r1.setLength(2);
r1.setWidth(6);
cout << "Rectangle#1:" << endl;
r1.show();
cout << "Area :" << r1.area() << endl;
cout << "Perimeter :" << r1.perimeter() <<
endl;
if (r1.sameArea(r4))
{
cout << "Rectangle#1 and Rectangle#4 have same area" <<
endl;
}
else
{
cout << "Rectangle#1 and Rectangle#4 have not same area"
<< endl;
}
return 0;
}
/**************************************************/
/**************************************************/
Output:
/**************************************************/