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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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:

int Rectangle::Area() { ... }

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.

  1. Implement the member function Area that returns the area of the Rectangle object.
  2. 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).
  3. 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);
Sample Output:
Rectangle 1
Please enter the length: 2
Please enter the width: 3
======
Rectangle 1 created with length 2 and width 3
The area of Rectangle 1 is: 6
The perimeter of Rectangle 1 is: 10
▬▬▬▬▬▬===
Rectangle 2
Please enter the length: 4
Please enter the width: 5
Rectangle 2 created with length
The area of Rectangle 2 is: 20
The perimeter of Rectangle 2 is: 18
and width 5
The largest rectangle has a length of 4, a width of 5, and an area of 20.
Transcribed Image Text:Sample Output: Rectangle 1 Please enter the length: 2 Please enter the width: 3 ====== Rectangle 1 created with length 2 and width 3 The area of Rectangle 1 is: 6 The perimeter of Rectangle 1 is: 10 ▬▬▬▬▬▬=== Rectangle 2 Please enter the length: 4 Please enter the width: 5 Rectangle 2 created with length The area of Rectangle 2 is: 20 The perimeter of Rectangle 2 is: 18 and width 5 The largest rectangle has a length of 4, a width of 5, and an area of 20.
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education