Skip to content

Instantly share code, notes, and snippets.

@imduffy15
Created May 23, 2013 13:10
Show Gist options
  • Select an option

  • Save imduffy15/5635948 to your computer and use it in GitHub Desktop.

Select an option

Save imduffy15/5635948 to your computer and use it in GitHub Desktop.
#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