Thread: Doesn't a derived class inherit the private member of its parent?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Doesn't a derived class inherit the private member of its parent?

    Hi all,

    I've got a base class Character and a class Pacman derived from Character.
    Character is defined as follows:

    Code:
    #ifndef CHARACTER_H
    #define CHARACTER_H
    
    #include <iostream>
    #include <string>
    #include "Position.h"
    
    enum STATE { DEAD = 0, ACTIVE = 1, SUPER = 2 };
    
    //Speed definitions
    const double NORMAL = 1.0;
    const double SLOW = 0.5;
    
    class Character
    {
    public:
       Character();
       Character(std::string name, Position position, int state, double speed);
       virtual ~Character();
       virtual void move();
       virtual void draw();
    
    private:
       std::string name;
       Position position;
       int state; // i.e. dead, active, super
       double speed;
    };
    
    #endif
    Code:
    #include "Character.h"
    
    using namespace std;
    
    Character::Character()
          : name(""), position(Position()), state(ACTIVE), speed(NORMAL)
    {}
    
    Character::Character(std::string name, Position position, int state, double speed)
          : name(name), position(position), state(state), speed(speed)
    {}
    
    Character::~Character()
    {}
    and Pacman is defined as follows:
    Code:
    #include "Pacman.h"
    
    using namespace std;
    
    Pacman::Pacman()
        : Character(), pillsTaken(0)
    { 
    	position.setX(STARTING_XPOSITION);
    	position.setY(STARTING_YPOSITION);
    }
    
    Pacman::Pacman(string name, Position position, int state,
    	double speed, int pillsTaken)
        : Character(), pillsTaken(0)
    {
    	position.setX(STARTING_XPOSITION);
    	position.setY(STARTING_YPOSITION);
    }
    and the compiler is giving me the following error:

    c:\users\...\arcade\pacman.cpp(8) : error C2248: 'Character:: position' : cannot access private member declared in class 'Character'
    c:\users\...\arcade\arcade\character.h(32) : see declaration of 'Character:: position'
    c:\users\...\arcade\character.h(22) : see declaration of 'Character'

    I thought that since Pacman is a child of Character, it will have its own copies of private attributes defined in Character. Is this not the case?
    Last edited by Canadian0469; 11-28-2008 at 10:48 PM. Reason: Added implementation of Character so you can see constructor defs in case they are what's problematic

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the "protected" keyword is similar to "private" but allows derived classes to access the variables and functions declared as such, which is what you would use in this case.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    No, private means private (i.e. nobody else can see them).
    If you want derived classes to have access to the variables then either call get/set functions in the base class or make the variables protected.
    protected only gives derived classes access to the variables, but not the outside world.
    Of course you could also make them public, but that would be very bad coding style.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Actually, you might not need to make position protected at all. For example, if the Position class has an appropriate constructor, you could just write:
    Code:
    Pacman::Pacman()
        : Character(std::string(), Position(STARTING_XPOSITION, STARTING_YPOSITION), ACTIVE, NORMAL),
            pillsTaken(0)
    {
    }
    By the way, I am guessing that the Character class is supposed to be an abstract base class, hence move() and draw() should be pure virtual member functions. Oh, and classes intended to be base classes should have a virtual destructor.

    Also, the STATE enum should belong to the Character class instead of being global. The same probably goes for the speed definitions (and if not, they should be declared extern and then defined in the source file).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Thanks laserlight, you're always so helpful!
    I did include a virtual destructor for base class character though didn't I?
    I have:
    virtual ~Character();

    isn't that the virtual destructor??

    Thanks a bunch

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Canadian0469
    isn't that the virtual destructor??
    Yes, that is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Quote Originally Posted by laserlight View Post
    .
    Also, the STATE enum should belong to the Character class instead of being global. The same probably goes for the speed definitions (and if not, they should be declared extern and then defined in the source file).
    Right, but for the speeds, the compiler complains (I'm using VC++ 2005) if I move the constant speed definitions into the class declaration since they are not of integral types.

    If I can't declare them in the class but they are only to be used for objects of type Character, what would be the correct way of providing the definitions?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If I can't declare them in the class
    You can declare them in the class, you just can't define them there. Put
    Code:
    const double Character::NORMAL = 1.0;
    const double Character::SLOW = 0.5;
    in a source file with static const declarations in the class.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    DOH!

    ...right

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 04-02-2006, 04:59 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM