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 C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information:     ...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Writ a program using java. Depending on the size of the room and the amount of...
Writ a program using java. Depending on the size of the room and the amount of shade that the room has, different sizes of air conditioning units must be used in order to be able to properly cool the room. The unit of measure for the amount of cooling that an air conditioner unit can provide is the BTU (British Thermal Unit) per hour. Code a program that will calculate the correct size of air conditioner for a specific room...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
(C++) I'm getting this message: "Programmer: Brooke Morris Course: ComSci 200 - 5047 libc++abi.dylib: terminating zsh:...
(C++) I'm getting this message: "Programmer: Brooke Morris Course: ComSci 200 - 5047 libc++abi.dylib: terminating zsh: abort ./a.out" when I run my program. What is the problem in my code? #include<iostream> #include<vector> #include<cstring> using namespace std; //leg class declaration class Leg { const char* const startCity; const char* const endCity; const double distance; friend class Routes; public: Leg(const char* const, const char* const, const double); Leg& operator= (const Leg&); double getDistance() const; void output(ostream&) const; }; //routes class declaration class...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT