Consider a super class show and derive the classes print and display from it, Write a program by showing the concept of function overriding by using Virtual Functions. code example
Example: virtual function in c++
#include <iostream>
#include<string>
class Entity {
public:
virtual std::string GetName() { return "Entity"; }
void Print() { std::cout << "This is Base class" << std::endl;}
};
class Player :public Entity {
std::string m_name;
public:
Player(const std::string& name)
:m_name(name)
{};
void Print() { std::cout << "This is Sub class" << std::endl; };
std::string GetName()override { return m_name; };
};
int main()
{
Entity* e = new Entity();
std::cout << e->GetName() << std::endl;
Player* p = new Player("Jacob");
std::cout << p->GetName() << std::endl;
PrintName(p);
Entity* notvirtualentity = new Entity();
Player* notvirtualpalyer = new Player("XX");
notvirtualentity = notvirtualpalyer;
notvirtualentity->Print();
std::cin.get();
}