Create a class Rectangle having width and length private members. The class certain a parameterised constructor for setting the width and length. It includes functions named clAire and calPerimeter that returns the area and perimeter of the Rectangle respectively. Also, overload the equality operator via friend function that compares whether two Rectangle objects are sunse or not.
In the main function, create two rectangle instances. Write necessary statement to call the above functions
#include <iostream>
#include <iomanip>
class Rectangle
{
int width_;
int length_;
public:
Rectangle(int width, int length) : width_(width), length_(length)
{
}
int calArea() const { return width_ * length_; }
int calPerimeter() const { return 2 * (width_ + length_); }
friend bool operator==(Rectangle&, Rectangle&);
};
bool operator==(Rectangle& r1, Rectangle& r2)
{
return r1.width_ == r2.width_ && r1.length_ == r2.length_;
}
int main()
{
Rectangle r1(2, 3);
Rectangle r2(3, 4);
std::cout << "Area of the rectangle 'r1' is " << r1.calArea() << "\n";
std::cout << "Area of the rectangle 'r2' is " << r2.calArea() << "\n";
std::cout << "Perimeter of the rectangle 'r1' is " << r1.calPerimeter() << "\n";
std::cout << "Perimeter of the rectangle 'r2' is " << r2.calPerimeter() << "\n";
std::cout << "Is rectangle 'r1' equal to rectangle 'r2': " << std::boolalpha << (r1 == r2) << "\n";
return 0;
}
Comments
Leave a comment