I need help with this simple coding activity. I will award full points to the fi
ID: 647330 • Letter: I
Question
I need help with this simple coding activity. I will award full points to the first person whos code works. Look for the comments in the code below to know which part to add to.
#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()
{
//
// Implement the Rectangle toString function
// using the Shape toString function
}
// Use the constructor you created
// for the previous problem here
//
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
#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()
{
string Shape::toString();
}
Rectangle(double w, double h, int s)
{
width = w;
height = 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;
}