In: Computer Science
I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks!
Creating a Rectangle class
We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following:
You then need to create a 'main' program and instantiate 3 rectangle objects with different parameters. You also need to print each rectangle's characteristics.
For this assignment you need to turn in 1 file which contains the class header and declaration/implementation, and the main program. Don't forget to include your file header.
Expected Output:
Your output shall look like this given the inputs below:
Enter the length of the 1st rectangle: 3
Enter the width of the 1st rectangle: 5
You created a rectangle with the following characteristics:
width = 5
length = 3
perimeter = 16
area = 15
Enter the length of the 2nd rectangle: 4
Enter the width of the 2nd rectangle: 6
You created a rectangle with the following characteristics:
width = 6
length = 4
perimeter = 20
area = 24
Enter the length of the 3rd rectangle: 4.1
Enter the width of the 3rd rectangle: 6.2
You created a rectangle with the following characteristics:
width = 6.2000
length = 4.1000
perimeter = 20.6000
area = 25.4200
EXTRA CREDIT
For 7 points of extra credit:
Store 10 rectangle objects you created in a vector and sequence through the vector to print out the 10 rectangle objects.
Screenshot
-----------------------------------------------
Program
rectangle.h
//Create a class rectangle
class Rectangle {
//Instance variables
private:
double length;
double width;
double perimeter;
double area;
//Member functions
public:
//Constructor
Rectangle(double len, double wid);
//Mutators
void setLength(double len);
void setWidth(double wid);
//Accessors
double getLength();
double getWidth();
double getArea();
double getPerimeter();
//Print object details
void print();
};
rectangle.cpp
#include "rectangle.h"
#include<iostream>
#include<iomanip>
using namespace std;
//Constructor
Rectangle::Rectangle(double len, double wid) {
length = len;
width = wid;
}
//Mutators
void Rectangle::setLength(double len) {
length = len;
}
void Rectangle::setWidth(double wid) {
width = wid;
}
//Accessors
double Rectangle::getLength() {
return length;
}
double Rectangle::getWidth() {
return width;
}
double Rectangle::getArea() {
area = (length*width);
return area;
}
double Rectangle::getPerimeter() {
perimeter = (2 * (length +
width));
return perimeter;
}
//Print object details
void Rectangle::print() {
cout <<setprecision(4);
cout << "\n\nwidth=" <<
width << "\nlength=" << length << "\nperimeter="
<< getPerimeter();
cout << "\narea=" <<
getArea() << endl << endl;
}
main.cpp
#include <iostream>
#include<vector>
#include"rectangle.h"
using namespace std;
int main()
{
//Variables for input read
double l, w;
/*//Create 3 objects of rectangle
for (int i = 0; i < 3; i++) {
cout << "Enter the length of
the " << (i + 1) << "th rectangle : ";
cin >> l;
cout << "Enter the width of
the "<<(i+1)<<"th rectangle : ";
cin >> w;
cout << "You created a
rectangle with the following characteristics : ";
Rectangle rect(l, w);
rect.print();
}*/
//Create vector to store object
vector<Rectangle> rectangles;
for (int i = 0; i < 10; i++) {
cout << "Enter the length of
the " << (i + 1) << "th rectangle : ";
cin >> l;
cout << "Enter the width of
the "<<(i+1)<<"th rectangle : ";
cin >> w;
Rectangle rect(l, w);
rectangles.push_back(rect);
}
//Print vector
for (int i = 0; i < 10; i++) {
cout << "You created a
rectangle with the following characteristics : ";
rectangles.at(i).print();
}
}
-----------------------------------------------------------
Output
Enter the length of the 1th rectangle : 1
Enter the width of the 1th rectangle : 2
Enter the length of the 2th rectangle : 2
Enter the width of the 2th rectangle : 3
Enter the length of the 3th rectangle : 3
Enter the width of the 3th rectangle : 4
Enter the length of the 4th rectangle : 4
Enter the width of the 4th rectangle : 5
Enter the length of the 5th rectangle : 5
Enter the width of the 5th rectangle : 6
Enter the length of the 6th rectangle : 6
Enter the width of the 6th rectangle : 7
Enter the length of the 7th rectangle : 7
Enter the width of the 7th rectangle : 8
Enter the length of the 8th rectangle : 8
Enter the width of the 8th rectangle : 9
Enter the length of the 9th rectangle : 9
Enter the width of the 9th rectangle : 10
Enter the length of the 10th rectangle : 10
Enter the width of the 10th rectangle : 11
You created a rectangle with the following characteristics :
width=2
length=1
perimeter=6
area=2
You created a rectangle with the following characteristics :
width=3
length=2
perimeter=10
area=6
You created a rectangle with the following characteristics :
width=4
length=3
perimeter=14
area=12
You created a rectangle with the following characteristics :
width=5
length=4
perimeter=18
area=20
You created a rectangle with the following characteristics :
width=6
length=5
perimeter=22
area=30
You created a rectangle with the following characteristics :
width=7
length=6
perimeter=26
area=42
You created a rectangle with the following characteristics :
width=8
length=7
perimeter=30
area=56
You created a rectangle with the following characteristics :
width=9
length=8
perimeter=34
area=72
You created a rectangle with the following characteristics :
width=10
length=9
perimeter=38
area=90
You created a rectangle with the following characteristics :
width=11
length=10
perimeter=42
area=110