(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
(1 - 2) Abstract Data Types D & D Chapter 12 Instructor - Andrew S. O’Fallon CptS 122 (June 10, 2015) Washington State University.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
CS212: Object Oriented Analysis and Design Lecture 13: Relationship between Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Topics Inheritance introduction
CITA 342 Section 1 Object Oriented Programming (OOP)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Object-Oriented Programming (OOP) and C++
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-Oriented Programming: Inheritance
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
(5 - 1) Object-Oriented Programming (OOP) and C++
Understanding Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Introduction Types Syntax Visibility Modes Example Programs
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Java Programming, Second Edition
(5 - 1) Object-Oriented Programming (OOP) and C++
Chapter 11: Inheritance and Composition
(4 – 2) Introduction to Classes in C++
Presentation transcript:

(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University

Key Concepts Base and derived classes Protected members Inheritance – public, protected, and private accessibility modes – is-a relationship – Single and multiple – Multilevel, hierarchical, and hybrid Software reuse through inheritance A. O’Fallon, J. Hagemeister 2

Introduction to Inheritance in OOP (I) Inheritance may be viewed as a form of software reuse Inheritance allows for the implementation of a class that acquires another class’ attributes and operations (its capabilities) – The class customizes or enhances the capabilities of the acquired class Software reuse allows for higher levels of developer production through leveraging tested, quality code A. O’Fallon, J. Hagemeister 3

Introduction to Inheritance in OOP (II) How inheritance works! – When implementing a new class some data members (attributes) and member functions (operations) might be in common between the new class and an existing class – the new class could inherit the members of the existing class The existing class is referred to as the base class (or superclass) The new class, which acquires the members, is referred to as the derived class (or subclass) – Represents a more customized or specialized version of objects A. O’Fallon, J. Hagemeister 4

Introduction to Inheritance in OOP (III) The is-a relationship represents inheritance For example: Let’s say we have a base class called Employee and a derived class called Manager – A Manager is an Employee (but, note, an Employee is not necessarily a Manager) In contrast the has-a relationship represents composition, where an object contains >= 1 objects of other classes as members Some possibilities include: – An Employee has a “dental plan” ( class DentalPlan), has an “office” ( class Office), etc. A. O’Fallon, J. Hagemeister 5

What is Inherited? A derived class inherits every member of a base class except its: – Constructor(s) – Destructor – Friend(s) – Overloaded assignment operator A. O’Fallon, J. Hagemeister 6

Base and Derived Classes Base classes tend to be more general Derived classes tend to be more specific We’ve established that every derived class is an object of it’s base class so… – The set of objects representative of the base class is usually larger than the set of objects representative of any of its derived classes An Employee class could be representative of all employee types including managers, supervisors, directors, officers, etc. A Manager class is a smaller, more specific subset of employees A. O’Fallon, J. Hagemeister 7

Protected Members The access specifier protected provides an intermediate level of protection between private and public Derived classes, and any of its friends, have access to protected members of a base class, but any nonmembers that are not friends do not have access A. O’Fallon, J. Hagemeister 8

Forms of Inheritance There are 5 forms of inheritance – Single – Multiple – Multilevel – Hierarchical – Hybrid A. O’Fallon, J. Hagemeister 9

Single Inheritance - Inheritance Structure of Employees of a Business (I) A. O’Fallon, J. Hagemeister 10 Employee Manager Single inheritance

Single Inheritance - Inheritance Structure of Employees of a Business (II) Single inheritance – One derived class inherits from only one base class – A Manager inherits capabilities of an Employee only – C++ syntax class Manager : public Employee { // class declarations }; A. O’Fallon, J. Hagemeister 11

Multiple Inheritance - Inheritance Structure of University Members (I) A. O’Fallon, J. Hagemeister 12 Student Worker TeachingAssistant Multiple inheritance

Multiple Inheritance - Inheritance Structure of University Members (II) Multiple inheritance – A derived class inherits from more than one base class – A TeachingAssistant inherits capabilities of a Worker and Student – C++ syntax class TeachingAssistant: public Worker, public Student { // class declarations }; A. O’Fallon, J. Hagemeister 13

Multilevel Inheritance - Inheritance Structure of Employees of a Business (I) A. O’Fallon, J. Hagemeister 14 Employee Officer Manager Employee is the direct base class of Manager Employee is an indirect base class of Officer Multilevel inheritance (3 levels)

Multilevel Inheritance - Inheritance Structure of Employees of a Business (II) Multilevel inheritance – A derived class acts as a base class for another derived class – An Officer is created from a Manager and a Manager is created from an Employee An Officer is a type of Manager and a Manager is a type of Employee – Generally want no more than a few levels A. O’Fallon, J. Hagemeister 15

Hierarchical Inheritance - Inheritance Structure of Employees of a Business (I) A. O’Fallon, J. Hagemeister 16 Officer COO CEO Hierarchical inheritance

Hierarchical Inheritance - Inheritance Structure of Employees of a Business (II) Hierarchical inheritance – Multiple derived classes inherit from the same base class – CEO (Chief Executive Officer) and COO (Chief Operations Officer) have attributes of an Officer, but also have their own unique attributes A. O’Fallon, J. Hagemeister 17

Hybrid Inheritance - Inheritance Structure of Employees of a Business (I) A. O’Fallon, J. Hagemeister 18 Officer COO CEO Hybrid inheritance Chairman The Diamond Problem!

Hybrid Inheritance - Inheritance Structure of Employees of a Business (II) Hybrid inheritance – Two or more inheritance forms are combined – A Chairman inherits from both CEO (Chief Executive Officer) and COO (Chief Operations Officer) classes, and CEO and COO inherit from Officer – forms a diamond relationship Here the “diamond” problem occurs because CEO and COO inherit from Officer, which have own copies of the data members and methods – Chairman contains two subobjects - there is ambiguity in which members are accessed by Chairman – We’ll solve this problem with keyword virtual – to be explained along with polymorphism later! A. O’Fallon, J. Hagemeister 19

Accessibility Modes and Inheritance in C++ (I) public, protected, and private – X in the table indicates hidden from derived class – table courtesy of tutorial-inheritance A. O’Fallon, J. Hagemeister 20

Accessibility Modes and Inheritance in C++ (II) public – C++ syntax class Manager : public Employee { // class declarations }; protected – C++ syntax class Manager : protected Employee { // class declarations }; private – C++ syntax class Manager : private Employee { // class declarations }; A. O’Fallon, J. Hagemeister 21

Summary of Inheritance (I) Advantages – Software reuse – Reduces code redundancy – Reduces code size – Promotes readability – Promotes extensibility Extensibility is a software design principle which considers growth of the system – a system’s ability to extend the system with new functionality with minimal changes and impact to the existing system’s functionality A. O’Fallon, J. Hagemeister 22

Summary of Inheritance (II) Disadvantages – Base classes and derived classes are tightly coupled – a change to the base class could impact all classes derived from it – With a class hierarchy, many data members could remain unused, possibly affecting performance A. O’Fallon, J. Hagemeister 23

In a Few Lectures… Soon we will discuss polymorphism! Let inheritance sink in first! A. O’Fallon, J. Hagemeister 24

A. O’Fallon, J. Hagemeister 25 References P.J. Deitel & H.M. Deitel, C: How to Program (7th ed.), Prentice Hall, 2013 J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (7 th Ed.), Addison- Wesley, 2013

A. O’Fallon, J. Hagemeister 26 Collaborators Jack Hagemeister