Insight 19.5.1.
Donโt Repeat Yourself: Copy and paste is a design failure! Part of the benefit of inheritance is reusing logic in the parent class.
introduce() themselves.
introduce() on a student object, that version will be used instead of the inherited one.
introduce() is overridden in the Student class. getName, which is not overridden, is only shown in the base class.
classDiagram
class Person {
-m_name: string
-m_age: int
+introduce()
+getName()
}
class Student {
-m_major: string
+introduce()
}
Person <|-- Student
Student version of introduce calls the Person version:
void introduce() const {
Person::introduce();
cout << "My major is " << m_major << "." << endl;
}
void introduce() const {
cout << "My major is " << m_major << "." << endl;
Person::introduce();
}
toString():
string toString() const {
string personStr = Person::toString();
return format("{}, Major: {}", personStr, m_major);
}
main could call s.Person::introduce() to get the Person version of the function instead of the Student version (assuming s is a Student). However, this is not a trick you should usually need very often.
Manager inherits from Employee which has a double calculatePay() const. Manager wants to override it to calculate the pay including a performance bonus. Build the function as it would look in Manager.