Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #include <iostream>
- using namespace std;
- class Shape {
- protected:
- int width, height;
- public:
- Shape(int a = 0, int b = 0) {
- width = a;
- height = b;
- }
- virtual void area() {
- cout << "Parent class area" << endl;
- }
- };
- class Rectangle : public Shape {
- public:
- Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
- void area() {
- cout << "Rectangle class area : " << (width * height) << endl;
- }
- };
- class Triangle : public Shape {
- public:
- Triangle(int a = 0, int b = 0) : Shape(a, b) {}
- void area() {
- cout << "Triangle class area : " << (width * height / 2) << endl;
- }
- };
- int main()
- {
- Shape *shape[4];
- Rectangle r1(5,3);
- Rectangle r2(25,7);
- Triangle t1(2, 6);
- Triangle t2(7, 9);
- shape[0] = &r1;
- shape[1] = &r2;
- shape[2] = &t1;
- shape[3] = &t2;
- for (int i = 0; i < 4; i++)
- {
- (*shape[i]).area();
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement