Lab: RectClass (constructor)
Code in C++
This program creates a Rectangle object, then displays the rectangle's
Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters.
Change this program to calculate and display the rectangle's
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:
Rectangle(){ width = 0; length = 0;} // default constructor
/* Write your code here */ // overloaded constructor
// 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()
{
// Demonstrate the default constructor
// Create a Rectangle object and use the default constructor to
assign values to its data members
Rectangle house1;
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house1.getWidth()
<< " feet wide.\n";
cout << "The house is " << house1.getLength()
<< " feet long.\n";
// Demonstrate the overloaded constructor
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 the overloaded constructor to
assign values to its data members
Rectangle house/* Write your code here */;
// Do not call the setters
// Display the house's width, length, and area.
cout << setprecision(2) << fixed << 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
return 0;
}
/***************************************************************
Save the OUTPUT below
*/
CODE:
#include <iostream>
#include <iomanip>
using namespace std;
double houseWidth; // To hold the room width
double houseLength; // To hold the room length
class Rectangle
{
private:
double width;
double length;
public:
Rectangle() {
width = 0;
length = 0;
} // default constructor
// getters
double getWidth() const {
return houseWidth;
}
double getLength() const {
return houseLength;
}
// other functions
double calculateArea() const {
return houseLength*houseWidth;
}
double pmeter(){
int a;
a=2*(houseWidth+houseLength);
return a;
}
};
int main()
{
Rectangle house1;
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house1.getWidth()<< "
feet wide.\n";
cout << "The house is " << house1.getLength()<< "
feet long.\n";
// 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;
Rectangle house;
cout << setprecision(2) << fixed << 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
cout<<"The house has "<<house.pmeter()<<" feet of
perimeter";
return 0;
}
SNAP-SHOT OF THE CODE IS BELOW FOR BETTER UNDERSTANDING:
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.