EEE 0116 Chapter 23 ObjectOriented Programming Inheritance 1
EEE 0116 Chapter 23: Object-Oriented Programming: Inheritance 1 C How to Program Deitel & Deitel
2 Outline Introduction Base Classes and Derived Classes protected Members Constructors and Destrcutors in Derived Classes
3 Introduction Inheritance is used to increase software reusability Inheritance is creating new class from an existing class Existing class’s data members and member functions are absorbed by new class New class is called as derived class Derived class represents more specialized group of objects Additional data members or member functions can be included in derived classes
4 Introduction Direct base class Inherited Indirect base class Inherited Single from one level up from two or more levels up inheritance Inherited Multiple from one base class inheritance Inherited from multiple base classes
5 Introduction Three types of inheritance public Every object of derived class is also an object of base class Base class objects are not objects of derived classes Derived class can access private base class members Inherited non-private member functions must be used to access private base class members private protected
6 Introduction “is-a” vs. “has-a” “is-a” is inheritance Derived class object can be considered as base class object “has-a” A is composition class contains one or more objects of other classes as data members.
7 Base Classes and Derived Classes Object of one class is an object of another class Base class represents larger set of objects than derived classes Example: Base class: Vehicle Derived class: Car
8 Base Classes and Derived Classes Inheritance Hierarchy Inheritance relationships can be considered as a tree-like hierarchy structure. Each class becomes base class or derived class. Base classes provides data and functionality to other classes Derived classes inherits data and functionality from other classes
9 Base Classes and Derived Classes public inheritance class Two. Dimensional. Shape : public Shape Two. Dimensional. Shape class inherits from Shape class Although not accessible directly, base class private members are still inherited They can be accessed using public member functions public and protected members inherited with original member access friend functions are not inherited
10 protected Members protected access is intermediate level of protection between public and private protected members are accessible to Base class members Base class friends Derived class members Derived class friends
11 Constructors and Destrcutors in Derived Classes
12 Constructors and Destrcutors in Derived Classes
13 Constructors and Destrcutors in Derived Classes
14 Constructors and Destrcutors in Derived Classes
15 Constructors and Destrcutors in Derived Classes
16 Constructors and Destrcutors in Derived Classes
17 Constructors and Destrcutors in Derived Classes
18 Constructors and Destrcutors in Derived Classes
19 Constructors and Destrcutors in Derived Classes
20 Constructors and Destrcutors in Derived Classes
21 Constructors and Destrcutors in Derived Classes
22 Constructors and Destrcutors in Derived Classes
23 Constructors and Destrcutors in Derived Classes
- Slides: 23