with C++ Lecture 8
Introduction to Inheritance
1. Concept of Inheritance
o Inheritance allows a new class to acquire the properties and
behaviors of an existing class. This mechanism enables code
reusability and the creation of a class hierarchy.
o Terminology:
Base Class (Parent): The class being inherited from.
Derived Class (Child): The class that inherits from the
base class.
Example:
2. Types of Inheritance Covered
o Public Inheritance: The derived class inherits public and
protected members of the base class but not private
members. Public inheritance maintains the access level of
the base class members in the derived class.
, The CPet Class Example
1. Class Definition
o A simple class CPet with a private data member mName, a
constructor to initialize the name, and setter and getter
functions.
Example:
2. Inheritance with CHamster Class
o The CHamster class inherits from CPet and extends its
functionality by adding a new data member mBreed and
additional methods.
Example: