Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define PI 3.14159265
- using namespace std;
- struct Coord {
- float x;
- float y;
- Coord() : x(0), y(0) {}
- };
- class Shape {
- protected:
- Coord center;
- public:
- /*Shape(){}*/
- virtual float area() = 0; // 面積 抽象類別
- virtual float perimeter() = 0; // 周長
- void Set(float x, float y)
- {
- center.x = x;
- center.y = y;
- }
- Coord GetCenter()
- {
- return center;
- }
- const Coord & GetCenterRef()
- {
- return center;
- }
- };
- class Reactangle : public Shape {
- public:
- float width;
- float height;
- public:
- Reactangle(float w, float h) : /*Shape(),*/ width(w), height(h) // 不寫 Shape() 會自動呼叫父類別的 constructor
- {
- }
- virtual float area()
- {
- return width * height;
- }
- virtual float perimeter()
- {
- return width + width + height + height;
- }
- };
- class Square : public Reactangle {
- public:
- Square(float w) : Reactangle(w, w)
- {
- }
- };
- class Circle : public Shape {
- public:
- float radius; // 半徑
- public:
- Circle(float r) : radius(r)
- {
- }
- float area()
- {
- return radius * radius * PI;
- }
- float perimeter()
- {
- return (radius + radius) * PI;
- }
- };
- class Ellipse : public Shape {
- private:
- float height;
- float width;
- public:
- Ellipse(float h, float w) : height(h), width(w)
- {
- }
- float area() {
- return (height/2) * (width/2) * PI;
- }
- float perimeter() {
- float res;
- res = (width > height) ? 2*PI*(height/2) + 4*(width-height) : 2*PI*(width/2) + 4*(height-width);
- return res;
- }
- };
- ostream& operator << (ostream & out, Coord c)
- {
- out << "(" << c.x << "," << c.y << ")";
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement