-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_virtual_function.cpp
44 lines (33 loc) · 1.18 KB
/
19_virtual_function.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
/* Abstract classes, virtual functions, pure virtual functions.. */
// # Abstract classes: Classes with pure virtual functions.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void attack(); // Virtual function definition is in respective inherited class. This just acts as a dummy template.
virtual void food() = 0; // Pure Virtual function signature. Compulsorily needs to be overwritten.
};
class Lion: public Animal {
public:
void attack() { // this method is invoked using lion class obj.
cout<<"Lion Attacked.!";
}
void food() { // Overwritten virtual function.
cout<<"Carnivorous";
}
};
class Tiger: public Animal {
public:
void attack() { // this method is invoked using tiger class obj.
cout<<"Tiger Attacked.!";
}
void food() { // Overwritten virtual function.
cout<<"Carnivorous";
}
};
int main() {
Lion lion;
Tiger tiger;
lion.attack();
tiger.attack();
}