Letβs try to fix that by making the member variables private and adding public constructors. Here is a first attempt. Scroll down to see the error message and the explanation of what is going wrong.
test.cpp:25:9: error: βstd::string Person::m_nameβ is private within this context
25 | m_name = name;
| ^~~~~~
That error is happening in the constructor of Student (highlighted in the code). Even though the Student class inherits from Person it cannot access the private members of Person. Recall that private is enforced at the class level, not for individual objects. One Person can access the private members of another Person, but a Student cannot access the private members of a Person (even though Student is-a Person).
Think back to the memory diagram on the previous page (FigureΒ 19.2.3). Inheritance means there is a Person inside of the Student object. That is different than a Student getting its own copy of all the members of a Personβa Student does not have a m_name.
One way to fix this is to do what we would with composition - to rely on public functions to access Person variables from Student. TIf we add a public Person::getName() function, we can call it in Student to access the name.
test.cpp: In constructor βStudent::Student(std::string, int, std::string)β:
test.cpp:24:49: error: no matching function for call to βPerson::Person()β
24 | Student(string name, int age, string major) {
| ^
This is the same issue we had with composition. The constructor for Student needs to set up the Person part of the object. Since we have not said how to do that, the compiler tries to automatically call the no-argument constructor for Person. But we donβt have a no-argument constructor for Person. So we get an error.
To fix this, we need to add a constructor to Person that takes the name and age as arguments. Then we can call that constructor from the Student constructorβs initialization list.
The : Person(name, age) on line 30 says βcall the constructor of the base class Person with the arguments name and ageβ. That means the code will not try to call the no-arg Person() constructor since we have specified how to initialize the base class. And it means that we donβt have to try to set the inherited private variables from inside Student.
We are writing a class Button that inherits from Input. The Input class has a private member variable string m_id and a constructor Input(string id). A Button will add a string m_text and a string m_color. Build the code for Button.