pure virtual function code example
Example 1: 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();
}
Example 2: what is abstract class in c++
//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;
}
Example 3: abstract class in c++
struct Abstract {
virtual void f() = 0; // pure virtual
}; // "Abstract" is abstract
struct Concrete : Abstract {
void f() override {} // non-pure virtual
virtual void g(); // non-pure virtual
}; // "Concrete" is non-abstract
struct Abstract2 : Concrete {
void g() override = 0; // pure virtual overrider
}; // "Abstract2" is abstract
int main()
{
// Abstract a; // Error: abstract class
Concrete b; // OK
Abstract& a = b; // OK to reference abstract base
a.f(); // virtual dispatch to Concrete::f()
// Abstract2 a2; // Error: abstract class (final overrider of g() is pure)
}