Sunday, February 25, 2018

C++ : Polymorphism and mutiple inheritence

#include <iostream>
using namespace std;
class Shape {
protected:
    int width, height;
public:
virtual int getArea(){}
    void setWidth(int w) { width = w; }
    void setHeight(int h) { height = h; }
};

class Rectangle : public Shape {
public:
    int getArea() { return (width * height); }
};

class Triangle : public Shape {
public:
    int getArea() { return (width * height) / 2; }
};

int main(void) {
    int x, y;
    cin >> y >> x;
    Rectangle Rect;
    Triangle Tri;
    Rect.setWidth(x);
    Rect.setHeight(y);
    Tri.setWidth(x);
    Tri.setHeight(y);
    Shape *shape[] = { &Rect, &Tri, 0 };
    Shape **pShape = &shape[0];
    while (*pShape)
    cout << (*pShape++)->getArea() << " ";
    return 0;
}

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows