forked from vectormars/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointer_026_pure_virtual.cpp
43 lines (42 loc) · 1.07 KB
/
Pointer_026_pure_virtual.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
// pure virtual function
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Base //base class
{
public:
virtual void show() = 0; //pure virtual function
};
////////////////////////////////////////////////////////////////
class Derv1 : public Base //derived class 1
{
public:
void show()
{
cout << "Derv1\n";
}
};
////////////////////////////////////////////////////////////////
class Derv2 : public Base //derived class 2
{
public:
void show()
{
cout << "Derv2\n";
}
};
////////////////////////////////////////////////////////////////
int main()
{
// Base bad; //can’t make object from abstract class
Base* arr[2]; //array of pointers to base class
Derv1 dv1; //object of derived class 1
Derv2 dv2; //object of derived class 2
arr[0] = &dv1; //put address of dv1 in array
arr[1] = &dv2; //put address of dv2 in array
arr[0]->show(); //execute show() in both objects
arr[1]->show();
return 0;
}
//Derv1
//Derv2