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};
26
27int main()
28{
29 //Entity a;//We can't do this because class Entity contains function that is unimplemented
30 Player x("Jacob");//This will work because we have implemented or defined the function in this sub class
31 std::cin.get();
32}
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
1struct Abstract {
2 virtual void f() = 0; // pure virtual
3}; // "Abstract" is abstract
4
5struct Concrete : Abstract {
6 void f() override {} // non-pure virtual
7 virtual void g(); // non-pure virtual
8}; // "Concrete" is non-abstract
9
10struct Abstract2 : Concrete {
11 void g() override = 0; // pure virtual overrider
12}; // "Abstract2" is abstract
13
14int main()
15{
16 // Abstract a; // Error: abstract class
17 Concrete b; // OK
18 Abstract& a = b; // OK to reference abstract base
19 a.f(); // virtual dispatch to Concrete::f()
20 // Abstract2 a2; // Error: abstract class (final overrider of g() is pure)
21}
1struct Abstract
2{
3 virtual ~Abstract() = 0;
4};
5
6Abstract::~Abstract() {}
7
8struct Valid: public Abstract
9{
10 // Notice you don't need to actually overide the base
11 // classes pure virtual method as it has a default
12};
13
14
15int main()
16{
17 // Abstract a; // This line fails to compile as Abstract is abstract
18 Valid v; // This compiles fine.
19}