La guía paso a paso o código que hallarás en este artículo es la solución más eficiente y válida que hallamos a tu duda o problema.
Ejemplo: interfaces en c++
#include #include //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 languagesclassEntitypublic://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 classvirtual 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;classPlayer:publicEntity
std::string m_name;public:Player(const std::string& name):m_name(name);voidPrint() 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;intmain()//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();
Te mostramos comentarios y valoraciones
Si sostienes algún reparo y disposición de modernizar nuestro artículo puedes ejecutar una nota y con gusto lo leeremos.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)