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.

Respuesta :

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;

}

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.