-
Notifications
You must be signed in to change notification settings - Fork 0
/
60.cpp
49 lines (43 loc) · 861 Bytes
/
60.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
#include <iostream>
using namespace std;
class base {
public:
virtual void vfunc()
{
cout << "This is base's vfunc().\n";
}
};
class derived1 : public base {
public:
void vfunc()
{
cout << "This is derived1's vfunc().\n";
}
};
class derived2 : public base {
public:
void vfunc()
{
cout << "This is derived2's vfunc().\n";
}
};
int main()
{
base *p, b;
derived1 d1;
derived2 d2;
// derived1 *p2;
// p2 = &b; //error
d1.vfunc();
d2.vfunc();
// point to base
p = &b;
p->vfunc(); // access base's vfunc()
// point to derived1
p = &d1;
p->vfunc(); // access derived1's vfunc()
// point to derived2
p = &d2;
p->vfunc(); // access derived2's vfunc()
return 0;
}