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

This is a discussion on Doesn't a derived class inherit the private member of its parent? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I've got a base class Character and a class Pacman derived from Character. Character is defined as follows: ...

  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 09: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
    21,433
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,433
    Quote Originally Posted by Canadian0469
    isn't that the virtual destructor??
    Yes, that is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,317
    >> 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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21