-
Notifications
You must be signed in to change notification settings - Fork 14
/
VirtualFunctions.cpp
57 lines (45 loc) · 1.24 KB
/
VirtualFunctions.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
#include<iostream>
//RUNTIME POLYMORPHISM - Virtual functions are used to achieve runtime polymorphism. It is achieved by function overriding(re-defination)
// of base class method which is re-defined in a derived class.
//Then a pointer of type base class is created which refers to derived class object
//and the base method is called , which actually executes the derived class version
//method.
//Dynamic binding is done i.e during runtime
using namespace std;
class Person {
public:
//does runtime binding of intro() based on the type of object is used to invoke the method
void virtual intro() {
cout<<"Hello I am a Person"<<endl;
}
};
class Anish : public Person {
public:
void intro() { //function overridden
cout<<"This is Anish"<<endl;
}
};
class Mrinal : public Person {
public:
void intro() {
cout<<"Hi this is Mrinal"<<endl;
}
};
void getIntro(Person &p) {
p.intro();
}
int main() {
Person *p1,*p2;
Anish a;
Mrinal m;
p1 = &a;
p2= &m;
p2 = &m;
//runtime polymorphism
p1->intro();//intro() method of class Anish called
p2->intro();//intro() method of class Mrinal called
// a.intro();
// getIntro(a);//without virtual base function , it would have printed the base method value
// getIntro(m);
return 0;
}