Assigned 9/15/2017-Due 9/18/2017 at NOON Introduction: In this in class assignme
ID: 3885952 • Letter: A
Question
Assigned 9/15/2017-Due 9/18/2017 at NOON Introduction: In this in class assignment, you will program a basic class for a rectangle. Here is what your class should include Private members length and width. Use the data type of double. . Default Constructor Overloaded constructor to accept length and width as arguments Public methods for . o Setting the length o Setting the width o Getting the length o Getting the width o Getting the area o Getting the perimeter In order to test your code, write a main function that Creates a first rectangle using the default constructor Sets the length and width of the first rectangle using the methods Displays the length, width, area, and perimeter of the first rectangle Creates a second rectangle using the overloaded constructor Displays the length, width, area, and perimeter of the second rectangle. Displays the sum of the perimeters Displays the difference between the areas . This submission MUST be done with 3 different files. Put the class definition in rectangle.h, the implementation of the methods in rectangle.cpp, and the main function in main.cpp. WRITE A MAKEFILE THAT WILL COMPILE THIS. In rectangle.h and rectangle.cpp, use comments to label your methods as either Constructors, Accessors or Mutators. Recommendations: .Get the class and implementations of the methods working in main.cpp. Once when your code is working, then worry about separating them into the different filesExplanation / Answer
#include<iostream>
using namespace std;
class Rectangle {
private:
int length;
int width;
public:
Rectangle(){
length = 0;
width = 0;
}
Rectangle(int a, int b){
length = a;
width = b;
}
void setLength(int a){
length = a;
}
void setWidth(int a){
width = a;
}
int getLength(){
return length;
}
int getWidth(){
return width;
}
int getArea(){
return length * width;
}
int getPerimeter(){
return 2 * (length + width);
}
};
int main(){
Rectangle r1;
r1.setLength(56);
r1.setWidth(28);
cout << "Rectangle 1:" << endl;
cout << "length:" << r1.getLength() << endl;
cout << "width:" << r1.getWidth() << endl;
cout << "area:" << r1.getArea() << endl;
cout << "Perimeter:" << r1.getPerimeter() << endl;
Rectangle r2(34,23);
cout << "Rectangle 2:" << endl;
cout << "length:" << r2.getLength() << endl;
cout << "width:" << r2.getWidth() << endl;
cout << "area:" << r2.getArea() << endl;
cout << "Perimeter:" << r2.getPerimeter() << endl;
cout << "Sum of perimeters :" << r1.getPerimeter()+r2.getPerimeter() << endl;
cout << "Area difference :" << r1.getArea() - r2.getArea() << endl;
return 0;
}