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};
26void PrintName(Entity* entity) {
27
28 std::cout << entity->GetName() << std::endl;
29}
30int main()
31{
32 //Entity a;//We can't do this because class Entity contains function that is unimplemented
33 Player x("Jacob");//This will work because we have implemented or defined the function in this sub class
34 std::cin.get();
35}
1//Code by Soumyadeep Ghosh
2//insta : @soumyadepp
3//linked in : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
4#include <bits/stdc++.h>
5
6using namespace std;
7
8class person
9{
10 string p_id;
11 public:
12 virtual void get_info()=0; //declaring person as abstract class
13 virtual void show()=0;
14};
15
16class student:public person
17{
18 string name;
19 int roll_no;
20 public:
21 /*overriding the pure virtual function declared in base class otherwise
22 this class will become an abstract one and then objects cannot be created
23 for the same*/
24 void get_info()
25 {
26 cout<<"Enter name of the student "<<endl;
27 cin>>name;
28 cout<<"Enter roll number of the student "<<endl;
29 cin>>roll_no;
30 }
31 void show()
32 {
33 cout<<"Name : "<<name<<" Roll number: "<<roll_no<<endl;
34 }
35};
36
37int main()
38{
39 person *p;
40 p=new student;
41 p->get_info();
42 p->show();
43 return 0;
44}
45
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>
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}