Question

Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...

Lab: RectClass (constructor)

Code in C++

This program creates a Rectangle object, then displays the rectangle's

  • length,
  • width, and
  • area

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

  • 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:
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


*/

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also...
(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also this should be in java) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. - A no-arg constructor that creates a default rectangle....
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
MATHEMATICS 1. The measure of location which is the most likely to be influenced by extreme...
MATHEMATICS 1. The measure of location which is the most likely to be influenced by extreme values in the data set is the a. range b. median c. mode d. mean 2. If two events are independent, then a. they must be mutually exclusive b. the sum of their probabilities must be equal to one c. their intersection must be zero d. None of these alternatives is correct. any value between 0 to 1 3. Two events, A and B,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT