10.1k views
2 votes
c++ program that creates three rectangle objects: the first with no width or height specific, the second with a width of 4 and a height of 40, and the third with a width of 3.5 and a height of 35.9. display the width, height, and perimeter for each rectangle object.

User MichAdel
by
5.0k points

1 Answer

3 votes

Answer:

#include <iostream>

using namespace std;

class Rectangle{

public:

double width, height;

public:

Rectangle();

Rectangle(double, double);

double perimeter() { return 2 * (width + height); }

};

Rectangle::Rectangle () {

width = 1.0;

height = 1.0;

}

Rectangle::Rectangle (double a, double b) {

width = a;

height = b;

}

int main()

{

Rectangle obj_rectangle1;

Rectangle obj_rectangle2(4,40);

Rectangle obj_rectangle3(3.5,35.9);

cout << "Rectangle1's width: " << obj_rectangle1.width << ", height: "<< obj_rectangle1.height << ", perimeter: " << obj_rectangle1.perimeter() << endl;

cout << "Rectangle2's width: " << obj_rectangle2.width << ", height: "<< obj_rectangle2.height << ", perimeter: " << obj_rectangle2.perimeter() << endl;

cout << "Rectangle3's width: " << obj_rectangle3.width << ", height: "<< obj_rectangle3.height << ", perimeter: " << obj_rectangle3.perimeter() << endl;

return 0;

}

Step-by-step explanation:

Declare two variables for width and height.

Specify the constructors and a function to calculate the perimeter.

Initialize the constructors (one with no parameter, and one with two parameters).

In the main, create the required objects and print the required values.

User Patrick C
by
5.3k points