Checkpoint 19.6.1.
Which is the correct statement about upcasting?
- Upcasting allows a derived class object to be treated as a base class object.
- Upcasting allows a base class object to be treated as a derived class object.
personRef2, which is a reference to a Person, but then assigns it to reference a Student:
Student is-a Person. A Person reference (or pointer) can refer (point) to a Student because a Student is just a specialized version of Person. Anytime we are working with a Student, it must also be true we are working with a Person.
Student& that refers to a Person. But doing so is a compile error because it makes no sense::
Person. In this sample, we call makeIntroduction twice. Once with a plain Person and then with a Person that happens to be a Student:
introduce(), not the customized Student version that includes the Studentβs major.
makeIntroduction function, the compiler needs to decide what introduce() to call. And it needs to generate one version of the code that will work for all possible types of Person. The only reasonable thing to do in that case is for the compiler to treat makeIntroduction as if it was written:
void makeIntroduction(const Person& person) {
cout << "Let's meet: " << person.getName() << endl;
person.Person::introduce(); // call Person version of introduce
}
makeIntroductions to not treat Student objects exactly the same as Person objects by using the customized version of .introduce(). But to do that, we need to tell C++ to wait until runtime to decide which version of introduce() to call. Which we will learn to do next.