-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37_pure_virtual_function.cpp
More file actions
56 lines (49 loc) · 1.05 KB
/
Copy path37_pure_virtual_function.cpp
File metadata and controls
56 lines (49 loc) · 1.05 KB
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
//example on pure virtual function or abstract class
//example on virtual function (ii)
#include <iostream>
#include <vector>
using namespace std;
class Animal
{
public :
virtual void speak() = 0; //Abstract class or pure virtual function //using virtual keyword
};
class Dog : public Animal
{
public :
void speak()
{
cout<<"Bow Bow"<<endl;
}
};
class Cat : public Animal
{
public :
void speak()
{
cout<<"Meow Meow"<<endl;
}
};
int main()
{
// Animal *p; //creating pointer
// p = new Dog();
// p->speak();
Animal *p;
vector<Animal*>animals; //using vector
animals.push_back(new Dog());
animals.push_back(new Cat());
animals.push_back(new Dog());
animals.push_back(new Cat());
for(int i=0;i<animals.size();i++)
{
p = animals[i];
p->speak();
}
//CLEANUP: Release all allocated heap memory
for(int i = 0; i < animals.size(); i++)
{
delete animals[i];
}
return 0;
}