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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.