Polymorphism not happening? c++

  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

Trying to get a hang of fully using OOP and I just get continually confused. Everything I learn reading an article or tutorial goes down the drain when it just doesn't work.

Here is the code I am using:

CPP Code: [ Select ]
#include <iostream>
#include <vector>
#include <windows.h>
 
/*
  Parent class to all objects which are part of the main game loop.
  Examples:
  static props
  physics props
  players
  enemies
  vehicles
  explosion handlers
  triggers
*/
class CgameObject{
public:
  long x,y; //location
  bool awake, visible;
 
  //constructor
  CgameObject(long setX,long setY){
    x = setX;
    y = setY;
    visible = true;
    awake = true;
  }
 
  virtual void step(){};
};
 
#include <cmath>
/*
  Objects which have basic motion properties,
  eg:
  players
  enemies
  vehicles
  physics props
*/
class CmotionObject : public CgameObject{
public:
  double direction, //angle in degrees going anticlockwise from the +ve x axis
  speed, //speed in pixels/frame along the direction (+ve or -ve)
  vSpeed, //speed in pixels/frame along the y axis, down being +ve
  hSpeed; //speed in pixels/frame along the x axis, right being +ve
 
public:
  //constructor
  CmotionObject(long setX,long setY):CgameObject(setX,setY){
    vSpeed = 0;
    hSpeed = 0;
    speed = 0;
    direction = 0;
  }
 
  virtual void applyMotion(){
    x += (long)hSpeed;
    y += (long)vSpeed;
  }
 
  /*
    No getters required I think.
  */
  virtual void setVSpeed(double newSpeed){
  }
 
  virtual void setHSpeed(double newSpeed){
  }
 
  virtual void setSpeed(double newSpeed){
  }
 
  virtual void setDirection(double newDirection){
  }
};
 
using namespace std;
unsigned int i;
unsigned long frameCount;
 
#include "classes/gameObject.h"
#include "classes/motionObject.h"
vector <CgameObject> gameObjects;
 
class Cplayer : public CmotionObject{
public:
  Cplayer(long setX,long setY):CmotionObject(setX,setY){};
 
  //do this every frame
  void step(){
    applyMotion();
    cout << "player is at " << x << endl;
    if(x < 100){
      if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
      hSpeed = 0; //stop moving
    }
  }
 
};
 
class Cnpc : public CmotionObject{
protected:
  int npcNumber;
 
public:
  //constructor
  Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
    npcNumber = setNpcNumber;
  }
 
  //do this every frame
  void step(){
    applyMotion();
    cout << "npc #" << npcNumber << " is at " << x << endl;
    if(x < 100){
      if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
      hSpeed = 0; //stop moving
    }
  }
 
};
 
int main(){
  frameCount = 0;
 
  gameObjects.push_back(Cplayer(0,30));
  gameObjects.push_back(Cnpc(1,0,10));
  gameObjects.push_back(Cnpc(2,0,20));
  gameObjects[0].hSpeed = 4;
 
  while(true){
    for(i = 0;i < gameObjects.size();i ++){
      gameObjects[i].step();
    }
    frameCount ++;
    cout << "frame #" << frameCount << " completed\n";
    Sleep(100); //10 fps
  }
  return 0;
}
 
 
  1. #include <iostream>
  2. #include <vector>
  3. #include <windows.h>
  4.  
  5. /*
  6.   Parent class to all objects which are part of the main game loop.
  7.   Examples:
  8.   static props
  9.   physics props
  10.   players
  11.   enemies
  12.   vehicles
  13.   explosion handlers
  14.   triggers
  15. */
  16. class CgameObject{
  17. public:
  18.   long x,y; //location
  19.   bool awake, visible;
  20.  
  21.   //constructor
  22.   CgameObject(long setX,long setY){
  23.     x = setX;
  24.     y = setY;
  25.     visible = true;
  26.     awake = true;
  27.   }
  28.  
  29.   virtual void step(){};
  30. };
  31.  
  32. #include <cmath>
  33. /*
  34.   Objects which have basic motion properties,
  35.   eg:
  36.   players
  37.   enemies
  38.   vehicles
  39.   physics props
  40. */
  41. class CmotionObject : public CgameObject{
  42. public:
  43.   double direction, //angle in degrees going anticlockwise from the +ve x axis
  44.   speed, //speed in pixels/frame along the direction (+ve or -ve)
  45.   vSpeed, //speed in pixels/frame along the y axis, down being +ve
  46.   hSpeed; //speed in pixels/frame along the x axis, right being +ve
  47.  
  48. public:
  49.   //constructor
  50.   CmotionObject(long setX,long setY):CgameObject(setX,setY){
  51.     vSpeed = 0;
  52.     hSpeed = 0;
  53.     speed = 0;
  54.     direction = 0;
  55.   }
  56.  
  57.   virtual void applyMotion(){
  58.     x += (long)hSpeed;
  59.     y += (long)vSpeed;
  60.   }
  61.  
  62.   /*
  63.     No getters required I think.
  64.   */
  65.   virtual void setVSpeed(double newSpeed){
  66.   }
  67.  
  68.   virtual void setHSpeed(double newSpeed){
  69.   }
  70.  
  71.   virtual void setSpeed(double newSpeed){
  72.   }
  73.  
  74.   virtual void setDirection(double newDirection){
  75.   }
  76. };
  77.  
  78. using namespace std;
  79. unsigned int i;
  80. unsigned long frameCount;
  81.  
  82. #include "classes/gameObject.h"
  83. #include "classes/motionObject.h"
  84. vector <CgameObject> gameObjects;
  85.  
  86. class Cplayer : public CmotionObject{
  87. public:
  88.   Cplayer(long setX,long setY):CmotionObject(setX,setY){};
  89.  
  90.   //do this every frame
  91.   void step(){
  92.     applyMotion();
  93.     cout << "player is at " << x << endl;
  94.     if(x < 100){
  95.       if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
  96.       hSpeed = 0; //stop moving
  97.     }
  98.   }
  99.  
  100. };
  101.  
  102. class Cnpc : public CmotionObject{
  103. protected:
  104.   int npcNumber;
  105.  
  106. public:
  107.   //constructor
  108.   Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
  109.     npcNumber = setNpcNumber;
  110.   }
  111.  
  112.   //do this every frame
  113.   void step(){
  114.     applyMotion();
  115.     cout << "npc #" << npcNumber << " is at " << x << endl;
  116.     if(x < 100){
  117.       if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
  118.       hSpeed = 0; //stop moving
  119.     }
  120.   }
  121.  
  122. };
  123.  
  124. int main(){
  125.   frameCount = 0;
  126.  
  127.   gameObjects.push_back(Cplayer(0,30));
  128.   gameObjects.push_back(Cnpc(1,0,10));
  129.   gameObjects.push_back(Cnpc(2,0,20));
  130.   gameObjects[0].hSpeed = 4;
  131.  
  132.   while(true){
  133.     for(i = 0;i < gameObjects.size();i ++){
  134.       gameObjects[i].step();
  135.     }
  136.     frameCount ++;
  137.     cout << "frame #" << frameCount << " completed\n";
  138.     Sleep(100); //10 fps
  139.   }
  140.   return 0;
  141. }
  142.  
  143.  


The error I am getting at the moment is:
error C2039: 'setHSpeed' : is not a member of 'CgameObject'

I understand that is the line:
gameObjects[0].hSpeed = 4;
And it makes sense that gameObjects do not have hSpeed as a member, but sure: motionObjects do! Is it not stated that:
"the type of the actual parameter can be a class derived from the class of the formal parameter"

Therefore I would surely think it would work? Without setting the hSpeed I get no errors but during runtime it is only running the step events of the base class, which does nothing.

edit:
This code is in my uni notes... see what I mean? Mine should work - or do vectors cause something strange to happen.
CPP Code: [ Select ]
class PS2Sprite
{
public:
   PS2Sprite();
   ~PS2Sprite();
   virtual void Render(void);
};
 
class PS2SpriteT : public PS2Sprite
{
public:
   PS2SpriteT();
   ~PS2SpriteT();
   void Render(void);
};
 
  1. class PS2Sprite
  2. {
  3. public:
  4.    PS2Sprite();
  5.    ~PS2Sprite();
  6.    virtual void Render(void);
  7. };
  8.  
  9. class PS2SpriteT : public PS2Sprite
  10. {
  11. public:
  12.    PS2SpriteT();
  13.    ~PS2SpriteT();
  14.    void Render(void);
  15. };
  16.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 74 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.