pure virtual function in c++
#include <iostream>
#include <string>
//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
class Entity {
public:
//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
//Below is an example a Pure Virtual Function
//It is an unimplemented function ant it forces the sub class to implement it and define it
//You will not be able to instantiate sub class without implementing or defining the function in sub class
virtual std::string GetName() = 0;
//the pure virtual function must have virtual written at the beginning and =0 at the end
//This function cannot contain any definition in base class,it is just a declaration
};
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; };//Pure virtual functions is implemented here in this sub class
};
void PrintName(Entity* entity) {
std::cout << entity->GetName() << std::endl;
}
int main()
{
//Entity a;//We can't do this because class Entity contains function that is unimplemented
Player x("Jacob");//This will work because we have implemented or defined the function in this sub class
std::cin.get();
}
4.33
3
//Code by Soumyadeep Ghosh
//insta : @soumyadepp
//linked in : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
#include <bits/stdc++.h>
using namespace std;
class person
{
string p_id;
public:
virtual void get_info()=0; //declaring person as abstract class
virtual void show()=0;
};
class student:public person
{
string name;
int roll_no;
public:
/*overriding the pure virtual function declared in base class otherwise
this class will become an abstract one and then objects cannot be created
for the same*/
void get_info()
{
cout<<"Enter name of the student "<<endl;
cin>>name;
cout<<"Enter roll number of the student "<<endl;
cin>>roll_no;
}
void show()
{
cout<<"Name : "<<name<<" Roll number: "<<roll_no<<endl;
}
};
int main()
{
person *p;
p=new student;
p->get_info();
p->show();
return 0;
}
Thank you!
3
0
4
6
#include <iostream>
#include<string>
//Virtual Functions are functions that allow us to override methods in subclasses
//In this example we have an entity class as a base class and class player inherits from public entity
class Entity {
public:
virtual std::string GetName() { return "Entity"; }//It is a method in base class that we want to modify in sub class Player
void Print() { std::cout << "This is Base class" << std::endl;}//function that is not virtual
};
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; };//function that is not virtual
std::string GetName()override { return m_name; };//overriding the function in sub class
};
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);// 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
//if It was not virtual it would have called The method From Entity Instance and output would be => Entity
Entity* notvirtualentity = new Entity();
Player* notvirtualpalyer = new Player("XX");
notvirtualentity = notvirtualpalyer;
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
std::cin.get();
}
Thank you!
6
0
4
9
#include <iostream>
#include <string>
class Entity {
public:
virtual std::string getName();
void print();
};
virtual std::string Entity::getName() {
return "Entity";
}
void Entity::print() {
std::cout << "This is the base class" << std::endl;
}
class Player : public Entity {
std::string m_name;
public:
Player(const std::string& name): m_name(name) {};
void print();
virtual std::string getName();
};
virtual std::string Player::getName() {
return m_name;
}
void Player::print() {
std::cout << "This is the sub class" << std::endl;
}
int main() {
Entity* e = new Entity();
std::cout << e->getName() << std::endl;
Player* p = new Player("Jacob");
std::cout << p->getName() << std::endl;
p->print();
e->print();
Entity* notVirtualEntity = new Entity();
Player* notVirtualPlayer = new Player("Bob");
notVirtualEntity = notVirtualPlayer;
notVirtualEntity->print();
notVirtualEntity->getName();
}
Thank you!
9
0
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks