Question

In: Computer Science

Rectangle Class in C++ Create a Rectangle class that has two data members: length_ and width_....

Rectangle Class in C++

Create a Rectangle class that has two data members: length_ and width_. Create the corresponding accessor and mutator functions to access and change both data members.

Create a member function area that returns the area of the Rectangle object. Finally, create a function longest_rectangle that accepts two Rectangle objects as parameters and returns the Rectangle object with the longer length.

Write the Rectangle class and the longest_rectangle function prototype in rectangle.hpp. Take note that longest_rectangle is not a member function of the Rectangle class. Provide the function's implementation in rectangle.cpp.

Complete main.cpp according to the comments provided in the file.

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

Rectangle 2
Please enter the length: 4
Please enter the width: 5
Rectangle 2 created with length 4 and width 5
The area of Rectangle 2 is: 20

The longest rectangle has a length of 4, a width of 5, and an area of 20.

Submission checklist

  1. Created function prototype and stored in .hpp file.
  2. Created function implementation and stored in .cpp file (see reference).
  3. Call function in the driver
  4. Compiled and ran the driver (main).
  5. Manually checked for compilation and logical errors.
  6. Ensured no errors on the unit test (make test).
  7. Followed advice from the stylechecker (make stylecheck).
  8. Followed advice from the formatchecker to improve code readbility (make formatcheck).

Solutions

Expert Solution

#######################################
            main.cpp
#######################################
#include <iostream>
#include "rectangle.hpp"


using namespace std;

Rectangle longest_rectangle(Rectangle r1, Rectangle r2) {
    if(r1.getLength() > r2.getLength()) {
        return r1;
    } else {
        return r2;
    }
}



int main() {
    int l, w;
    Rectangle r1, r2;
    cout << "Rectangle 1" << endl;
    cout << "Please enter the length: ";
    cin >> l;    
    cout << "Please enter the width: ";
    cin >> w;
    r1.setLength(l);
    r1.setWidth(w);

    cout << "Area of rectangle 1 is: " << r1.getArea() << endl;
    
    cout << "Rectangle 2" << endl;
    cout << "Please enter the length: ";
    cin >> l;    
    cout << "Please enter the width: ";
    cin >> w;
    r2.setLength(l);
    r2.setWidth(w);
    
    cout << "Area of rectangle 2 is: " << r1.getArea() << endl;

    Rectangle result = longest_rectangle(r1, r2);
    cout << "The longest rectangle has a length of " << result.getLength() << ", a width of " << result.getWidth() << ", and an area of " << result.getArea() << endl;
}



#######################################
       rectangle.cpp
#######################################
#include "rectangle.hpp"

Rectangle::Rectangle(){
    this->length_ = 0;
    this->width_ = 0;
}
int Rectangle::getLength() {
    return this->length_;
}
void Rectangle::setLength(int length_) {
    this->length_ = length_;
}
int Rectangle::getWidth() {
    return this->width_;
}
void Rectangle::setWidth(int width_) {
    this->width_ = width_;
}
int Rectangle::getArea() {
    return this->width_ * this->length_;
}
ostream& operator<<(ostream &out, Rectangle &rectangle) {
    out << "length_ " << rectangle.length_ << endl;
    out << "width_ " << rectangle.width_ << endl;
    return out;
}




#######################################
       rectangle.hpp
#######################################
#ifndef _RECTANGLE_H_
#define _RECTANGLE_H_
#include <iostream>

using namespace std;

class Rectangle {
    private:
    int length_;
    int width_;

    public:
    Rectangle();
    int getLength();
    void setLength(int);
    int getWidth();
    void setWidth(int);
    friend ostream& operator<<(ostream &out, Rectangle &rectangle);
    int getArea();
};

#endif



**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
I. Design and code a class Rectangle that has the following properties: Two private members double...
I. Design and code a class Rectangle that has the following properties: Two private members double length and width A constructor that accepts two parameters for the private members Default constructor that sets both members to 0 Setters/getters for each private member Method area() that returns the area of the rectangle Method Perim() that returns the perimeter of the rectangle Method toString that return the param as a String Method Add that accepts a Rectangle and returns a rectangle with...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT