1#include <iostream>
2#include <string>
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
4//Pure virtual function is also called an interface in other languages
5class Entity {
6public:
7 //virtual std::string GetName() { return "Entity"; }//This is a function that is just virtual .Overriding this function in sub class is optional we can instantiate subcllass without overriding or implementing this function
8
9 //Below is an example a Pure Virtual Function
10 //It is an unimplemented function ant it forces the sub class to implement it and define it
11 //You will not be able to instantiate sub class without implementing or defining the function in sub class
12 virtual std::string GetName() = 0;
13 //the pure virtual function must have virtual written at the beginning and =0 at the end
14 //This function cannot contain any definition in base class,it is just a declaration
15};
16class Player :public Entity {
17 std::string m_name;
18
19public:
20 Player(const std::string& name)
21 :m_name(name)
22 {};
23 void Print() { std::cout << "This is Sub class" << std::endl; };
24 std::string GetName()override { return m_name; };//Pure virtual functions is implemented here in this sub class
25};
26
27int main()
28{
29 //Entity a;//We can't do this because class Entity contains function that is unimplemented
30 Player x("Jacob");//This will work because we have implemented or defined the function in this sub class
31 std::cin.get();
32}