Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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();
}
Comment

virtual function in c++

#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();
}
Comment

C++ virtual function

class Base {
   public:
    void print() {
        // code
    }
};

class Derived : public Base {
   public:
    void print() {
        // code
    }
};
Comment

virtual function in C++

class base{
public:
 virtual void fun()=0;
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: descending order c++ 
Cpp :: Inner Section Sticky Scroll in elementor 
Cpp :: struct c++ 
Cpp :: slice a vector c++ 
Cpp :: Max element in an array with the index in c++ 
Cpp :: array length c++ 
Cpp :: c++ function as paramter 
Cpp :: c++ float and double 
Cpp :: c++ clip values 
Cpp :: how to initialize 2d array with values c++ 
Cpp :: cpp ifdef 
Cpp :: cpp auto 
Cpp :: vector of vectors of pairs c++ 
Cpp :: swap in cpp 
Cpp :: c++ how to return an empty vector 
Cpp :: polymorphism in c++ 
Cpp :: passing custom function in sort cpp 
Cpp :: looping in map c++ 
Cpp :: Bucket and Water Flow codechef solution in c++ 
Cpp :: set of vectors c++ 
Cpp :: max pooling in c++ 
Cpp :: hierarchical inheritance in c++ employee 
Cpp :: find the graph is minimal spanig tree or not 
Cpp :: c++ pass ofstream as argument 
Cpp :: declare a tab c++ 
Cpp :: tabeau dynamique c++ 
Cpp :: string append at position c++ 
Cpp :: memset in cpp 
Cpp :: minimum or maximum in array c++ 
Cpp :: c++ pointers 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =