- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPureVirtualFunction.cpp
More file actions
Latest commit
74 lines (62 loc) · 1.74 KB
/
Copy pathPureVirtualFunction.cpp
File metadata and controls
74 lines (62 loc) · 1.74 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Pure Virtual Function
* 1. All pure virtual functions must be implemented in each subclass.
* 2. We cannot create an object of a class which contains a pure virtual function.
* 3. Pure virtual function or inteface allows us to define a function in a base class that doesn't have an implementation or definition in the base class and
force sub classes to implement that function. Pure virtual function is also called an interface in other languages.
4. The pure virtual function must have virtual written at the beginning and =0 at the end.
5. It cannot contain any definition in base class,it is just a declaration.
6. A class can have more than one pure virtual functions.
7. We cannot create objects but we can create pointer of the pure vitual function class.
*/
#include<iostream>
classEntity {
public:
Entity() { }
voidshow() {
std::cout << "Pure Virtual Function" << std::endl;
}
virtual std::string GetName() = 0;
virtualintGetAge() = 0;
};
classStudent : publicEntity {
private:
std::string name;
public:
Student() = default;
Student(const std::string& name) {
this->name = name;
}
std::string GetName() override {
return name;
}
intGetAge() override {
return23;
}
};
classEmployee : publicStudent {
private:
std::string name;
public:
Employee(const std::string& name) {
this->name = name;
}
std::string GetName() override {
return name;
}
intGetAge() override {
return77;
}
};
voidPrintName(Entity* entity) {
std::cout << entity->GetName() << std::endl;
std::cout << entity->GetAge() << std::endl;
}
intmain()
{
Student* student = newStudent("Joy Saha"); // student type pointer
Employee* emp = newEmployee("Sir");
Entity* en = nullptr;
en->show();
PrintName(student);
PrintName(emp);
}