Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We have provided you with a Shape parent class, and a Rectangle class that inher

ID: 3681274 • Letter: W

Question

We have provided you with a Shape parent class, and a Rectangle class that inherits from it. Implement the Rectangle constructor.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Shape
{
public:
   Shape(double w, double h);

protected:
   double width;
   double height;

};

Shape::Shape(double w, double h)
{
   width = w;
   height = h;
}

class Rectangle : public Shape
{

public:
   Rectangle(double w, double h, int s);
   string toString();

private:
   int sides;

};

string Rectangle::toString()
{
   stringstream ss;
   ss << "Width: " << width << endl;
   ss << "Height: " << height << endl;
   ss << "Sides: " << sides << endl;

   return ss.str();
}

//
// Using the Shape class constructor
// implement the Rectangle constructor
//

int main()
{
   double width;
   double height;
   const int sides = 4;

   cin >> width;
   cin >> height;

   Rectangle bob = Rectangle(width, height, sides);
   cout << bob.toString();

   return 0;
}

Explanation / Answer

constructor:

Rectangle::Rectangle(double w, double h, int s)::Shape(w,h){
   sides=s;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote