List of all available Websheets
Viewing cpp/cs104/polymorphism/shapes by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Complete the derived shape classes. |
Engine | |
Template / Reference solution |
using namespace std; class Shape { public: virtual ~Shape() { } virtual double getArea() = 0; virtual double getPerimeter() = 0; virtual string getType() = 0; }; class RightTriangle : \[ REDACTED ]\ { public: RightTriangle(double b, double h) \[ REDACTED ]\ ~RightTriangle() { } double getArea() { return \[ REDACTED ]\ } double getPerimeter() { return sqrt(_b*_b + _h*_h) + _h + _b; } string getType() { return "Right Triangle"; } private: \[ REDACTED ]\ }; class Rectangle : \[ REDACTED ]\ { public: Rectangle(double b, double h) \[ REDACTED ]\ ~Rectangle() { } double getArea() { return _b * _h; } double getPerimeter() { return \[ REDACTED ]\ ; } string getType() { return "Rectangle"; } private: \[ REDACTED ]\ }; class Square : \[ REDACTED ]\ { public: Square(double s) \[ REDACTED ]\ // Override other virtual functions as needed \[ REDACTED ]\ }; int main() { vector<Shape *> shapeList; int selection = -1; while(selection != 0){ cout << "Choose an option:" << endl; cout << "=================" << endl; cout << "Enter '0' to quit" << endl; cout << "Enter '1 base height' for a right triangle with given base and height" << endl; cout << "Enter '2 base height' for a rectangle with given base and height" << endl; cout << "Enter '3 side' for a square with given side length" << endl; cout << "> "; cin >> selection; // Right Triangle case if(selection == 1){ double b, h; cin >> b >> h; shapeList.push_back(new \[ REDACTED ]\ ); } // Rectangle case else if(selection == 2){ double b, h; cin >> b >> h; // Add the rest of the code to allocate a new rectangle // and add it to the shapeList shapeList.push_back(new \[ REDACTED ]\); } // Square case else if(selection == 3){ double s; cin >> s; // Add the rest of the code to allocate a new square // and add it to the shapeList shapeList.push_back(new \[ REDACTED ]\ ); } } cout << endl; for (vector<Shape *>::iterator it = shapeList.begin() ; it != shapeList.end(); ++it) { Shape *s = *it; cout << s->getType() << ": Area=" << s->getArea() << " Perim=" << s->getPerimeter() << endl; delete s; } return 0; } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [{"stdin":"1 2 4\n2 3 4\n3 5\n0"}] |
Solution visibility | |
Is example? i.e., just a demo |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.