Содержание
- 2. What Is Inheritance? Provides a way to create a new class from an existing class The
- 3. Example: Insect Taxonomy CS1 -- Inheritance and Polymorphism
- 4. The "is a" Relationship Inheritance establishes an "is a" relationship between classes. A poodle is a
- 5. Inheritance – Terminology and Notation in C++ Base class (or parent) – inherited from Derived class
- 6. Back to the ‘is a’ Relationship An object of a derived class 'is a(n)' object of
- 7. What Does a Child Have? An object of the derived class has: all members defined in
- 8. Protected Members and Class Access protected member access specification: like private, but accessible by objects of
- 9. Class Access Specifiers public – object of derived class can be treated as object of base
- 10. Inheritance vs. Access CS1 -- Inheritance and Polymorphism
- 11. Inheritance vs. Access CS1 -- Inheritance and Polymorphism
- 12. Inheritance vs. Access CS1 -- Inheritance and Polymorphism
- 13. Inheritance vs. Access CS1 -- Inheritance and Polymorphism
- 14. Constructors and Destructors in Base and Derived Classes Derived classes can have their own constructors and
- 15. Constructors and Destructors in Base and Derived Classes CS1 -- Inheritance and Polymorphism
- 16. Constructors and Destructors in Base and Derived Classes CS1 -- Inheritance and Polymorphism
- 17. Constructors and Destructors in Base and Derived Classes CS1 -- Inheritance and Polymorphism
- 18. Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to
- 19. Passing Arguments to Base Class Constructor CS1 -- Inheritance and Polymorphism Square::Square(int side):Rectangle(side,side) derived class constructor
- 20. Redefining Base Class Functions Redefining function: function in a derived class that has the same name
- 21. Redefining Base Class Functions Not the same as overloading – with overloading, parameter lists must be
- 22. Base Class CS1 -- Inheritance and Polymorphism
- 23. Derived Class CS1 -- Inheritance and Polymorphism Redefined setScore function
- 24. Driver Program CS1 -- Inheritance and Polymorphism
- 25. Problem with Redefining Consider this situation: Class BaseClass defines functions x() and y(). x() calls y().
- 26. Problem with Redefining CS1 -- Inheritance and Polymorphism BaseClass DerivedClass void X(); void Y(); void Y();
- 27. Class Hierarchies CS1 -- Inheritance and Polymorphism A base class can be derived from another base
- 28. Class Hierarchies CS1 -- Inheritance and Polymorphism Consider the GradedActivity, FinalExam, PassFailActivity, PassFailExam hierarchy in Chapter
- 29. Polymorphism and Virtual Member Functions Virtual member function: function in base class that expects to be
- 30. Polymorphism and Virtual Member Functions CS1 -- Inheritance and Polymorphism Because the parameter in the displayGrade
- 31. CS1 -- Inheritance and Polymorphism
- 32. CS1 -- Inheritance and Polymorphism As you can see from the example output, the getLetterGrade member
- 33. Static Binding Program 15-10 displays 'C' instead of 'P' because the call to the getLetterGrade function
- 34. Virtual Functions A virtual function is dynamically bound to calls at runtime. At runtime, C++ determines
- 35. Virtual Functions To make a function virtual, place the virtual key word before the return type
- 36. Updated Version of GradedActivity CS1 -- Inheritance and Polymorphism The function is now virtual. The function
- 37. Polymorphism CS1 -- Inheritance and Polymorphism If we recompile our program with the updated versions of
- 38. CS1 -- Inheritance and Polymorphism
- 39. CS1 -- Inheritance and Polymorphism
- 40. Polymorphism Requires References or Pointers Polymorphic behavior is only possible when an object is referenced by
- 41. Base Class Pointers CS1 -- Inheritance and Polymorphism Can define a pointer to a base class
- 42. Base Class Pointers Base class pointers and references only know about members of the base class
- 43. Redefining vs. Overriding In C++, redefined functions are statically bound and overridden functions are dynamically bound.
- 44. Virtual Destructors It's a good idea to make destructors virtual if the class could ever become
- 45. Abstract Base Classes and Pure Virtual Functions Pure virtual function: a virtual member function that must
- 46. Abstract Base Classes and Pure Virtual Functions Abstract base class: class that can have no objects.
- 47. Multiple Inheritance CS1 -- Inheritance and Polymorphism A derived class can have more than one base
- 49. Скачать презентацию