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:32:9: error: โstd::string Person::m_nameโ is private within this context
32 | m_name = name;
| ^~~~~~
That error is happening in the constructor of Student (line 32, 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. If 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:31:49: error: no matching function for call to โPerson::Person()โ
31 | Student(string name, int age, string major) {
| ^
We saw a similar issue when exploring constructors and 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 automatically calls 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 call the Person constructor from the Student constructorโs initialization list. To do so, we add : Person(name, age) after the constructor signature.
The : Person(name, age) on line 30 says โcall the constructor of the base class Person with the arguments name and ageโ. Because we are making that call, the compiler wonโt try to call the no-argument constructor for Person (which doesnโt exist).
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.