In: Computer Science
6.6 Lab: Rectangle Class
This program creates a Rectangle object, then displays the rectangle's
length,
width, and
area
Change this program to calculate and display the rectangle's
perimeter.
Example:
In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00 square feet of area. The house has 90.00 feet of perimeter.
/**
This program creates a Rectangle object, then displays the
rectangle's
- length,
- width, and
- area
Change this program to calculate and display the rectangle's
- perimeter.
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle
{
private:
double width;
double length;
public:
// setters
void setWidth(double w) { width = w; }
void setLength(double len) {length = len; }
// getters
double getWidth() const { return width; }
double getLength() const { return length; }
// other functions
double calculateArea() const { return width * length; }
/* Write your code here */
};
int main()
{
double houseWidth; // To hold the room width
double houseLength; // To hold the room length
// Get the width of the house.
cout << "In feet, how wide is your house? ";
cin >> houseWidth;
// Get the length of the house.
cout << "In feet, how long is your house? ";
cin >> houseLength;
// Create a Rectangle object and use setters to assign values to
its data members
Rectangle house;
house.setWidth(houseWidth);
house.setLength(houseLength);
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house.getWidth()
<< " feet wide.\n";
cout << "The house is " << house.getLength()
<< " feet long.\n";
cout << "The house has " << house.calculateArea()
<< " square feet of area.\n";
// Display the perimeter below
/* Write your code here */
return 0;
}
/***************************************************************
Save the OUTPUT below
*/
//Change the code only ever so slighty, not using advanced
algorithims
CODE:


OUTPUT:

RAW CODE:
#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle
{
private:
double width;
double length;
public:
// setters
void setWidth(double w) { width = w; }
void setLength(double len) {length = len; }
// getters
double getWidth() const { return width; }
double getLength() const { return length; }
// other functions
double calculateArea() const { return width * length; }
/* Write your code here */
double calculatePerimeter() const {
return 2*( width + length ) ;
}
};
int main()
{
double houseWidth; // To hold the room width
double houseLength; // To hold the room length
// Get the width of the house.
cout << "In feet, how wide is your house? ";
cin >> houseWidth;
// Get the length of the house.
cout << "In feet, how long is your house? ";
cin >> houseLength;
// Create a Rectangle object and use setters to assign values to its data members
Rectangle house;
house.setWidth(houseWidth);
house.setLength(houseLength);
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house.getWidth() << " feet wide.\n";
cout << "The house is " << house.getLength() << " feet long.\n";
cout << "The house has " << house.calculateArea() << " square feet of area.\n";
cout << "The house has " << house.calculatePerimeter() << " square feet of perimeter.\n" ;
return 0;
}
NOTE:
If You have any doubts feel free to comment in comment
section.
DO VOTE(LIKE).