-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial22.cpp
56 lines (50 loc) · 1.01 KB
/
tutorial22.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
#include<bits/stdc++.h>
using namespace std;
/*
POLYMORPHISM - many forms
1. Polymorphism can be classified into two categories : Compile Time and Run Time.
2. Compile Time Polymorphism can be achieved using Function Overloading and Operator Overloading.
3. Run Time Polymorphism is achieved using virtual functions.
*/
// Run time polymorphism - Virtual Functions
class Human
{
public:
virtual void speak()
{
cout<<"Human is speaking."<<endl;
}
void sing()
{
cout<<"Human is singing."<<endl;
}
};
class Student : public Human
{
public:
void speak()
{
cout<<"Student is speaking."<<endl;
}
void sing()
{
cout<<"Student is singing."<<endl;
}
};
void LetsSpeak(Human &h) // Do not forget to receive as reference
{
h.speak();
}
void LetsSing(Human &h) // Do not forget to receive as reference
{
h.sing();
}
int main()
{
Student saurav;
LetsSpeak(saurav);
LetsSing(saurav);
Human nishant;
LetsSpeak(nishant);
LetsSing(nishant);
}