-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualFunc.cpp
More file actions
59 lines (49 loc) · 791 Bytes
/
virtualFunc.cpp
File metadata and controls
59 lines (49 loc) · 791 Bytes
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
58
59
#include<iostream>
using namespace std;
class A{
public:
int a;
void hello(){
cout<<"hello A"<<endl;
}
virtual void bye(){
cout<<"bye A"<<endl;
}
};
class B : public A{
public:
virtual void bye(){
cout<<"bye B"<<endl;
}
void hello(){
cout<<"hello B"<<endl;
}
};
class C : public A{
public:
virtual void bye(){
cout<<"bye C"<<endl;
}
void hello(){
cout<<"hello C"<<endl;
}
};
int main()
{
A a;
A *p;
p = &a;
p->bye();
B b;
C c;
p = &b;
//function overriding in a dynamic way using virtual functions
p->bye();
p = &c;
p->bye();
a.hello();
b.hello();
c.hello();
p->hello();
return 0;
}