Created
May 23, 2013 13:10
-
-
Save imduffy15/5635948 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class Shape { | |
| protected: | |
| float area; | |
| float perimeter; | |
| public: | |
| virtual void display(); | |
| virtual string getShapeName() = 0; | |
| }; | |
| class Rectangle : public Shape { | |
| public: | |
| Rectangle(float width, float height); | |
| virtual string getShapeName(); | |
| }; | |
| class Circle : public Shape { | |
| public: | |
| Circle(float radius); | |
| virtual string getShapeName(); | |
| }; | |
| // Part 1 | |
| void Shape::display() { | |
| cout << "Shape: " << getShapeName() << "\n" | |
| << "Area: " << this->area << "\n" | |
| << "Perimeter: " << this->perimeter << "\n"; | |
| } | |
| Rectangle::Rectangle(float width, float height) { | |
| this->area = width*height; | |
| this->perimeter = width+width+height+height; | |
| } | |
| string Rectangle::getShapeName() { | |
| return "Rectangle"; | |
| } | |
| Circle::Circle(float radius) { | |
| this->area = 3.14 * radius * radius; | |
| this->perimeter = 2 * 3.14 * radius; | |
| } | |
| string Circle::getShapeName() { | |
| return "Circle"; | |
| } | |
| // Part 2 | |
| int main() { | |
| Rectangle rec = Rectangle(4,10); | |
| rec.display(); | |
| cout << "\n"; | |
| Circle cir = Circle(10); | |
| cir.display(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment