Answer to Question #170901 in C++ for Soniya_ 24

Question #170901

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


1
Expert's answer
2021-03-11T08:02:58-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS