-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolymorphism.cpp
60 lines (51 loc) · 1.47 KB
/
Polymorphism.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;
// Interface
class Shape
{
public:
// IMP: Empty Virtual Destructor for proper cleanup
virtual ~Shape() { }
virtual void printShape() = 0;
virtual int getArea() = 0;
virtual int getCircumference() = 0;
};
class Square : public Shape
{
int _length;
public:
Square(int len) : _length(len) { }
~Square() { }
virtual void printShape() { cout << "Square:: Len: " << _length; }
virtual int getArea() { return _length * _length; }
virtual int getCircumference() { return 4 * _length; }
};
class Rectangle : public Shape
{
int _length;
int _breadth;
public:
Rectangle(int len, int wid) : _length(len),
_breadth(wid) { }
~Rectangle() { }
virtual void printShape() { cout << "Rectangle:: Len: " << _length << "; Wid: " << _breadth; }
virtual int getArea() { return _length * _breadth; }
virtual int getCircumference() { return 2 * (_length + _breadth); }
};
int main()
{
vector<Shape*> shapes;
shapes.push_back(new Square(5));
shapes.push_back(new Square(10));
shapes.push_back(new Rectangle(5, 10));
shapes.push_back(new Rectangle(15, 10));
for (Shape* s : shapes)
{
s->printShape();
cout << "; Area: " << s->getArea();
cout << "; Circum: " << s->getCircumference() << endl;
}
cout << endl;
return 0;
}