Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Shape
- {
- protected:
- int width, height;
- public:
- Shape(int a, int b)
- {
- width = a;
- height = b;
- }
- virtual int area() = 0;
- };
- // Rectangle : -
- class Rectangle : public Shape{
- public :
- Rectangle(int a, int b) : Shape(a, b){}
- int area()
- {
- cout << "Rectangle class area :" << (width * height) << endl;
- }
- };
- // Triangle : -
- class Triangle : public Shape
- {
- public:
- Triangle(int a, int b) : Shape(a, b) {}
- int area()
- {
- cout << "Triangle class area :" << (width * height / 2) << endl;
- }
- };
- int main()
- {
- Shape *shape;
- Rectangle rec(20, 14);
- Triangle tri(20, 10);
- // store the address of rectangle
- shape = &rec;
- // call rectangle area.
- shape->area();
- // store the address of triangle
- shape = &tri;
- // call triangle area.
- shape->area();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement