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
/*
An example to illustrate the use of virtual specifier
For Q/A: https://www.sololearn.com/Discuss/814374/virtual
*/
#include <iostream>
#include <random>
using namespace std;
class Figure {
public:
/* Try commenting out the virtual method and uncommenting the non-virtual one. See the result. */
/* Declaring a pure virtual draw() method, as we can't draw a GENERIC shape */
virtual void draw() = 0;
/* Non virtual method. Has no sense and will not work as it should. Adding 'virtual' before the declaration will solve the case, but still, no point in drawing an unknown shape */
//void draw() {cout << "Drawing some nonsense for 'a' shape" << endl;}
};
class Rectangle : public Figure {
public:
void draw() {
cout << "Rectangle was drawn" << endl;
}
};
class Triangle : public Figure {
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run