Derived class doubt

Hi there,

So, if I have a class Person, in which the elements are private (instead of protected), do I have to define them again in the derived class?
Here's the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Person
{ 
public: 
  Person(string name, unsigned int age); 
  string getName() const; 
  unsigned int getAge() const; 
  // other methods 
private:          //private, NOT PROTECTED!
  string name; 
  unsigned int age; 
};



class Player : public Person
{
public:
        Player();
        Player(std::string name, unsigned int age, std::string team);
        std::string getTeam();
        //outras funções
private:
        std::string team;
};
  
//Here's the constructor:
Player::Player(std::string name, unsigned int age, std::string team) : Person(name, age)   //Here's the question, can I do this?
{
        this->team = team;
}
Last edited on
do I have to define them again in the derived class?

No. That's the point of inheritance. The derived class inherits the member variables and functions of the base class.
Last edited on
Thank you very much, I was in doubt if I could do this if the main class was private and not protected.
Thanks again! :D
Keep in mind that if the member variables in the base class are private, the derived class won't be able to access them directly. Access must be done through getters, setters, or constructors (line 27).
Topic archived. No new replies allowed.