1#include <iostream>
2#include<string>
3 //Virtual Functions are functions that allow us to override methods in subclasses
4//In this example we have an entity class as a base class and class player inherits from public entity
5class Entity {
6public:
7 virtual std::string GetName() { return "Entity"; }//It is a method in base class that we want to modify in sub class Player
8 void Print() { std::cout << "This is Base class" << std::endl;}//function that is not virtual
9};
10class Player :public Entity {
11 std::string m_name;
12
13public:
14 Player(const std::string& name)
15 :m_name(name)
16 {};
17 void Print() { std::cout << "This is Sub class" << std::endl; };//function that is not virtual
18 std::string GetName()override { return m_name; };//overriding the function in sub class
19};
20
21int main()
22{
23 Entity* e = new Entity();
24 std::cout << e->GetName() << std::endl;
25 Player* p = new Player("Jacob");
26 std::cout << p->GetName() << std::endl;
27 PrintName(p);// This function calls the GetName method from the Player instance despite it takes an entity instance as a parameter this is because player class is a sub class of Entity and the method is marked virtual it will map with the method in the Player class and call it from there .It outputs => Jacob
28 //if It was not virtual it would have called The method From Entity Instance and output would be => Entity
29 Entity* notvirtualentity = new Entity();
30 Player* notvirtualpalyer = new Player("XX");
31 notvirtualentity = notvirtualpalyer;
32 notvirtualentity->Print();//It prints => this is base class if it was virtual function it would call print function from Player Class and print => This is subclass
33 std::cin.get();
34}
1#include <iostream>
2struct Base {
3 virtual void f() {
4 std::cout << "base\n";
5 }
6};
7struct Derived : Base {
8 void f() override { // 'override' is optional
9 std::cout << "derived\n";
10 }
11};
12int main()
13{
14 Base b;
15 Derived d;
16
17 // virtual function call through reference
18 Base& br = b; // the type of br is Base&
19 Base& dr = d; // the type of dr is Base& as well
20 br.f(); // prints "base"
21 dr.f(); // prints "derived"
22
23 // virtual function call through pointer
24 Base* bp = &b; // the type of bp is Base*
25 Base* dp = &d; // the type of dp is Base* as well
26 bp->f(); // prints "base"
27 dp->f(); // prints "derived"
28
29 // non-virtual function call
30 br.Base::f(); // prints "base"
31 dr.Base::f(); // prints "base"
32}
1#include <iostream>
2#include <string>
3
4class Entity {
5public:
6 virtual std::string getName();
7 void print();
8};
9
10virtual std::string Entity::getName() {
11 return "Entity";
12}
13
14void Entity::print() {
15 std::cout << "This is the base class" << std::endl;
16}
17
18class Player : public Entity {
19 std::string m_name;
20public:
21 Player(const std::string& name): m_name(name) {};
22 void print();
23 virtual std::string getName();
24};
25
26virtual std::string Player::getName() {
27 return m_name;
28}
29
30void Player::print() {
31 std::cout << "This is the sub class" << std::endl;
32}
33
34int main() {
35 Entity* e = new Entity();
36 std::cout << e->getName() << std::endl;
37 Player* p = new Player("Jacob");
38 std::cout << p->getName() << std::endl;
39 p->print();
40 e->print();
41
42 Entity* notVirtualEntity = new Entity();
43 Player* notVirtualPlayer = new Player("Bob");
44 notVirtualEntity = notVirtualPlayer;
45 notVirtualEntity->print();
46 notVirtualEntity->getName();
47}