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

Sacred Thought

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...