CS 23001 CS II Data Structures and Abstraction
CS 23001 CS II: Data Structures and Abstraction Inheritance & Dynamic Type Resolutions Xiang Lian Department of Computer Science Kent State University Email: xlian@kent. edu Homepage: http: //www. cs. kent. edu/~xlian/ 1
Objectives • Inheritance – Example – Types – Syntax 2
Introduction • Object-Oriented Programming in C++ – Data encapsulation – Inheritance 3
An Example of Inheritance 4
Base Class vs. Derived Class • Base class (a. k. a. parent class, superclass) – the existing class from which a new class inherits members – responsible for sharing its members with its derived class(es) • Derived class (a. k. a. child class, subclass) – the new class that inherited from the base class – responsible for inheriting some or all of the members of the base class(es) 5
Importance of Inheritance in C++ • It reduces code redundancy – It allows the derived classes to inherit members of the base class • It offers the code reusability • … 6
Types of Inheritance 1. Single Inheritance 2. Multilevel Inheritance 3. Hierarchical Inheritance 4. Multiple Inheritance 5. Hybrid Inheritance (a. k. a. Virtual Inheritance) 7
Syntax • Single Inheritance class Base { // BODY OF THE BASE CLASS }; class Derived : acess_specifier Base { // BODY OF THE DERIVED CLASS }; public private protected 8
Syntax (cont'd) • Multilevel Inheritance class A // Base class { // BODY OF CLASS A }; class B : acess_specifier A // Derived class of A { // BODY OF CLASS B }; class C : access_specifier B // Derived from derived class B { // BODY OF CLASS C }; 9
Syntax (cont'd) • Hierarchical Inheritance class A // Base class of B { // BODY OF THE PROGRAM }; class B : access_specifier A // Derived class of A { // BODY OF THE PROGRAM }; class C : access_specifier A // Derived class of A { // BODY OF THE PROGRAM }; class D : access_specifier B // Derived class of B { // BODY OF THE PROGRAM }; 10
Syntax (cont'd) • Multiple Inheritance class B // Base class of A { // BODY OF THE CLASS B }; class C // Base class of A { // BODY OF THE CLASS C }; class A : acess_specifier B, access_specifier C // Derived class of B and C { // BODY OF CLASS A }; 11
Syntax (cont'd) • Hybrid Inheritance class A { // BODY OF THE CLASS A }; class B : acess_specifier A { // BODY OF THE CLASS D }; class C : acess_specifier A { // BODY OF THE CLASS C }; class D : acess_specifier B, acess_specifier C { // BODY OF THE CLASS D }; Multilevel & Hierarchical Inheritance 12
Visibility Modes • The public visibility mode allows the derived class to access the public and protected but not private members of the base class 13
Visibility Modes (cont'd) • The private visibility mode allows the derived class to access the public and protected members of the base class privately. 14
Visibility Modes (cont'd) • The protected visibility mode allows the derived class to access the public and protected members of the base class in the protected mode. 15
Summary of Visibility Modes 16
Static and Dynamic Type Resolution • Static Binding – Happens at compile time – E. g. , binding function call to the correct function depending on parameter types • Dynamic Binding – Happens at run time 17
Example of Dynamic Binding 18
Virtual Function in C++ • A virtual function is a member function – declared within a base class – re-defined (overriden) by a derived class • Virtual functions ensure that the correct function is called for an object • Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism 19
Example of Virtual Functions 20
Code • shape class – virtual void draw () = 0; • • • rectangle square triangle circle … 21
22
- Slides: 22