Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
C++ Inheritance Systems Programming.
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.
Object-Oriented PHP (1)
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Chapter 15 What is Inheritance?
Inheritance, Polymorphism, and Virtual Functions
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
An Object-Oriented Approach to Programming Logic and Design
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.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
A First Book of C++ Chapter 12 Extending Your Classes.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Class Inheritance Part I
Today’s Objectives 5-Jul-2006 Announcements
Object-Oriented Programming: Inheritance
Object-Oriented Programming
Inheritance AKEEL AHMED.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Understanding Inheritance
Learning Objectives Inheritance Virtual Function.
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
By Muhammad Waris Zargar
Java Programming, Second Edition
Chapter 11: Inheritance and Composition
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected Member Data Base Class Access Specifications The Base Class Constructor Calls Overriding Member Functions

Inheritance (continue) Types of Inheritance More Examples of Simple Inheritance Examples of Multi-Level Inheritance Examples of Multiple Inheritance Homework and Lab Assignment

Concept of Inheritance What is inheritance? - to inherit properties/capabilities (member data and member functions) from an existing class or existing classes. - the existing class is called as a base class - the inherited class is called as a derived class - The two classes in the inheritance must have relationship in terms of code-reusable

Concept of Inheritance (continue) Why Inheritance? - code reusability (save time and money) - Increasing program reliability - Better problem-solving and program design - Supporting polymorphism

Concept of Inheritance (continue) Conceptual examples - example 1: circle base class: circle area = *r*r derived class: sphere sphere area = 4 *circle::area volume = 4/3*circle::area*r Sphere is kind of circular shape r r

Concept of Inheritance (continue) - example 2: base class: employee members: ID,name, salary employee() show() derived class: manager members: manager is kind of employee office, bonus manager() computeBonus() show()

Simple Examples of Inheritance Example of inheriting a circle class to a sphere class - \CS116\YgaoHandouts\Ch9\circlein.cpp Example of inheritance - (p ) Textbook example

Base Classes and Derived Classes Characteristics of a base class - has all features of the objects in the category - is a general description of all objects in the category in terms of member data and member functions Characteristics of a derived class - has detailed and specific features of the objects in the class - usually has more capabilities than the base class in terms of member data and member functions Examples: - circlesphereemployeemanager

Private Member Data vs. Protected Member Data Define the member data as protected in a base class if the derived class can directly access the data as it own member data Define the member data as private in a base class if the derived class can only access the data via the member functions defined in the base class. Example: CS116/Ygao/Handouts /Ch9/circlein.cpp

Base Class Access Specifications

Base Class Access Specifications (continues)

The Base Class Constructor Calls Explicit base constructor calls i.e., shpere(double radius) : circle(radius) { } The base class’s constructor is called before the derived class’s constructor. The destructors are called in reverse order. i.e., (p ) – textbook example

Overriding Member Functions The functions in a base class can be overridden in the derived classes Use the scope resolution operator :: to indicate the member functions called are from the base class. i.e., direct member function call area = 2*circle::Area() + Area(); the base member function call Must be used internally in the member functions of the derived class (p ) – textbook examples

Types of Inheritance Basically, there are three types of inheritance - simple inheritance: a base class derives on or many derived classes - multiple-level inheritance: a derived class can be served as a base class for next level of inheritance - multiple inheritance: there are more than one base class to derive one or many derived classess.

Types of Inheritance (continue) Example of simple inheritance

Types of Inheritance (continue) Example of multiple-level inheritance

Types of Inheritance (continue) Example of multiple inheritance

More Examples of Simple Inheritance /CS116/YGao/Handouts/Ch9/inherivd.cpp

Example of Multiple-level Inheritance /CS116/YGao/Handouts/Ch9/multilevel.cpp (p ) – textbook example

Example of Multiple Inheritance (p ) textbook example