Advertisement
TermSpar

C++ Inheritance

Jun 7th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. //Parent Class:
  8. class Displayable{
  9. public:
  10.     virtual void display();
  11. };
  12.  
  13. void Displayable::display(){
  14.     cout << "OBJECT DISPLAYED\n\n";
  15. }
  16.  
  17. //Subclass:
  18. class Player : public Displayable{
  19. public:
  20.     virtual void display();
  21. };
  22.  
  23. void Player::display(){
  24.     cout << "PLAYER DISPLAYED\n\n";
  25. }
  26.  
  27. //Subclass:
  28. class Enemy : public Displayable{
  29. public:
  30.     virtual void display();
  31. };
  32.  
  33. void Enemy::display(){
  34.     cout << "ENEMY DISPLAYED\n\n";
  35. }
  36.  
  37. int main(){
  38.    
  39.     vector<Displayable*> myGame;
  40.  
  41.     myGame.push_back(new Player());
  42.     myGame.push_back(new Enemy());
  43.  
  44.     for(vector<Displayable*>::iterator iter = myGame.begin(); iter != myGame.end(); ++iter){
  45.         (*iter)->display();
  46.     }
  47.  
  48.     system("pause");
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement