In: Computer Science
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
#######################################
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.