Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- using namespace std;
- class shape {
- public:
- virtual double get_area()const = 0;
- virtual double get_perimeter()const = 0;
- virtual ~shape() { }
- };
- class circle : public shape {
- public:
- circle(double);
- virtual double get_area()const;
- virtual double get_perimeter()const;
- private:
- circle() { }
- double radius;
- };
- class rectangle : public shape {
- public:
- rectangle(double, double);
- virtual double get_area()const;
- virtual double get_perimeter()const;
- private:
- rectangle() { }
- double width;
- double height;
- };
- class triangle : public shape {
- public:
- triangle(double, double, double);
- virtual double get_area()const;
- virtual double get_perimeter()const;
- private:
- triangle() { }
- double a;
- double b;
- double c;
- };
- int main() {
- const shape * ptr = NULL;
- const circle c(3.25);
- const rectangle r(7.12, 4.3);
- const triangle t(2.65, 3.15, 2.88);
- ptr = &c;
- cout << " Sc = " << ptr->get_area() << "\tPc = " << ptr->get_perimeter() << endl;
- ptr = &r;
- cout << " Sr = " << ptr->get_area() << "\tPr = " << ptr->get_perimeter() << endl;
- ptr = &t;
- cout << " St = " << ptr->get_area() << "\tPt = " << ptr->get_perimeter() << endl;
- cout << endl;
- const size_t size = 3;
- const shape * descendants[size];
- descendants[0] = &c;
- descendants[1] = &r;
- descendants[2] = &t;
- for (rsize_t n = 0; n < size; n++)
- cout << " S = " << descendants[n]->get_area() << "\tP = " << descendants[n]->get_perimeter() << endl;
- cin.get();
- return 0;
- }
- circle::circle(double _r) : radius(_r) { }
- double circle::get_perimeter()const { return 2 * M_PI * radius; }
- double circle::get_area()const { return M_PI * radius * radius; }
- rectangle::rectangle(double _w, double _h) : width(_w), height(_h) { }
- double rectangle::get_perimeter()const { return (width + height) * 2; }
- double rectangle::get_area()const { return width * height; }
- triangle::triangle(double _a, double _b, double _c) : a(_a), b(_b), c(_c) { }
- double triangle::get_perimeter()const { return a + b + c; }
- double triangle::get_area()const {
- double p = get_perimeter() / 2;
- return sqrt(p * (p - a) * (p - b) * (p - c));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement