CPP
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
#include <iostream>
using namespace std;
//draw function comes from Shape
class Shape{
public:
virtual void draw() = 0;
};
//inherits Shape and implements draw
class Circle : public Shape{
public:
void draw() { cout << "circle\n"; }
};
//inherits Shape and implements draw
class Triangle : public Shape{
public:
void draw() { cout << "triangle\n"; }
};
//Drawing class does not have shapes but uses Shape as function argument. So the functionality can be extended without needing to alter the class. The specific information is injected by external Shape classes.
class Drawing{
public:
void drawShape(Shape *pShape){
pShape->draw();
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run