Computer Science 1037A/B Lecture 39: Type Compatibility in Inheritance Hierarchies

29 views3 pages

Document Summary

Classes in a program may be part of an inheritance hierarchy. Classes lower in the hierarchy are special cases of those above. A derived class pointer can always be assigned to a base class pointer. Assigning a base class pointer to a derived class pointer requires a cast. The base class pointer must already point to a derived class object for this to work. C++ uses the declared type of a pointer to determine access to the members of the pointed-to object. If an object of a derived class is pointed to by a base class pointer, all members of the derived class may not be accessible. Type cast the base class pointer to the derived class (via static_cast) in order to access members that are specific to the derived class. #include using namespace std; enum discipline { archaeology, biology, computer_science }; enum classification { freshman, sophomore, junior, senior }; class person protected: string name; public:

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents