Implement the member function Area that returns the area of the Rectangle object. Implement the member function Perimeter that returns the perimeter of the Rectangle object. (Note that the perimeter is equal to the total sum of the length of all of a rectangle's sides). Implement the function LargestRectangleByArea to find the larger of two input Rectangles by area.
Rectangle Class
Observe the Rectangle class in rectangle.h, and you'll see it has two member variables: length_ and width_. These member variables can be modified with the "Setter functions" SetLength and SetWidth (aka mutator functions) and can be retrieved with the "getter functions" GetLength and GetWidth (aka accessor functions).
You will be implementing two member functions of the Rectangle class: Area, and Perimeter.
Notice that if a function belongs to a class, when we define the function outside of the class, we indicate that it is a member function of a class using the :: operator. For example, because Area() belongs to the Rectangle class, when we define the function in rectangle.cc, it is written as:
After implementing these two member functions, you will implement a function, LargestRectangleByArea that compares two input Rectangle objects and returns which of the two is larger, based on its area.
- Implement the member function Area that returns the area of the Rectangle object.
- Implement the member function Perimeter that returns the perimeter of the Rectangle object. (Note that the perimeter is equal to the total sum of the length of all of a rectangle's sides).
- Implement the function LargestRectangleByArea to find the larger of two input Rectangles by area.
Complete main.cc according to the comments provided in the file. Note that bold values in the samples represent input by the user.
main.cc file
#include <iostream> | |
#include "rectangle.h" | |
int main() { | |
std::cout << "====== Rectangle 1 ======" << std::endl; | |
// ===================== YOUR CODE HERE ======================== | |
// Accept user input for the length and width of rectangle 1, | |
// and instantiate a new Rectangle object with these inputs. | |
// ============================================================= | |
std::cout << "====== Rectangle 2 ======" << std::endl; | |
// ===================== YOUR CODE HERE ======================== | |
// Accept user input for the length and width of rectangle 2, | |
// and instantiate a new Rectangle object with these inputs. | |
// ============================================================= | |
// ===================== YOUR CODE HERE ======================== | |
// Call LargestRectangleByArea to determine which rectangle | |
// is larger, and print out its length, width, and area. | |
// Follow the README for formatting. | |
// ============================================================= | |
return0; | |
} |
rectangle.cc file
#include "rectangle.h" | |
unsigned int Rectangle::Area() const { | |
// ===================== YOUR CODE HERE ======================== | |
// Compute the area of this rectangle object. | |
// Remember that member functions can access the member variables of the | |
// class. Hint: look at `rectangle.h` to see the member variables you can | |
// access. | |
// ============================================================= | |
return0; | |
} | |
unsigned int Rectangle::Perimeter() const { | |
// ===================== YOUR CODE HERE ======================== | |
// Compute the perimeter of this rectangle object. | |
// ============================================================= | |
return0; | |
} | |
Rectangle LargestRectangleByArea(Rectangle &r1, Rectangle &r2) { | |
// ===================== YOUR CODE HERE ======================== | |
// Compare the areas of the two given rectangles, and return | |
// the Rectangle whose area is larger. | |
// ============================================================= | |
return r1; | |
} |
rectangle.h file
class Rectangle { | |
public: | |
// Setter functions of the Rectangle class. | |
voidSetLength(unsignedint length) { | |
length_ = length; | |
} | |
voidSetWidth(unsignedint width) { | |
width_ = width; | |
} | |
// Getter functions of the Rectangle class. | |
unsignedintGetLength() const { | |
return length_; | |
} | |
unsignedintGetWidth() const { | |
return width_; | |
} | |
// Other member functions of the Rectangle class. | |
unsignedintArea() const; | |
unsignedintPerimeter() const; | |
private: | |
// Member variables of the Rectangle class. | |
unsignedint length_; | |
unsignedint width_; | |
}; | |
Rectangle LargestRectangleByArea(Rectangle &r1, Rectangle &r2); |
Setter and Getter method:
We know that the private member of the class cannot be accessed outside the class by the name of the variable. The setter-and-getter methods of a class provide an alternative way to set and get the private members of the class outside. In the solution of the given problem, we use two setter methods SetLength() and SetWidth() to set the value of class member length_ and width_, and two getter methods: GetLength() and GetWidth() to fetch the value of class member length_ and width_ within the main function.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images