pure virtual function in C++
class base{
public:
virtual void fun(){
}
};
pure virtual function in C++
class base{
public:
virtual void fun(){
}
};
virtual function in C++
class base{
public:
virtual void fun()=0;
};
virtual function c++
#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();
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us