PLEASE HELP!
Mar 31, 2016 at 4:36am UTC
How do i use the parent Shape class' toString function to implement the Rectangle toString function. It should print out the same information, and in the same format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Shape
{
public :
Shape(double w, double h);
string toString();
private :
double width;
double height;
};
Shape::Shape(double w, double h)
{
width = w;
height = h;
}
string Shape::toString()
{
stringstream ss;
ss << "Width: " << width << endl;
ss << "Height: " << height << endl;
return ss.str();
}
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();
}
Rectangle::Rectangle(double w, double h, int s)
:Shape(w,h)
{
sides= s;
}
//
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;
Mar 31, 2016 at 6:47am UTC
For instance:
1 2 3 4 5 6 7 8 9
string Rectangle::toString()
{
//
stringstream ss;
ss <<Shape::toString(); // Note: This is how you call a function of the base class
ss << "Sides: " << sides<<endl;
return ss.str();
}
Mar 31, 2016 at 5:37pm UTC
thanks!
Topic archived. No new replies allowed.