I need help with this assignment We have provided you with a Shape parent class,
ID: 3765633 • Letter: I
Question
I need help with this assignment
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
Sample code:
Sample output:
code to copy:
#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)//shape parameterized constructor
{
width = w;
height = h;
}
class Rectangle:public Shape//inherit class shape
{
public:
Rectangle(double w,double h,int s);
string toString();
private:
int sides;
};
Rectangle::Rectangle(double w,double h,int s):Shape(w,h)//implementation of rectangle constuctor
{
sides=s;
}
///tostring method definition
string Rectangle::toString()
{
stringstream ss;
ss << "Width: " << width << endl;
ss << "Height: " << height << endl;
ss << "Sides: " << sides << endl;
return ss.str();
}
int main()
{
double width;
double height;
const int sides = 4;
cout<<"enter width:";
cin>>width;
cout<<"enter height:";
cin>>height;
Rectangle bob=Rectangle(width,height,sides);
cout<<bob.toString();
return 0;
}