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.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
Object Oriented Programming: Inheritance Chapter 9.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Object-Oriented Programming: Inheritance and Polymorphism.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

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 Inheritance Hierarchy 6 Constructors and Destructors in Derived Classes 7 Uses A and Knows A Relationships 8 public, protected and private Inheritance 9 Software Engineering with Inheritance

Inheritance Software reusability Create new class from existing class Absorb existing classs data and behaviors Enhance with new capabilities Derived class inherits from base class Derived class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors

Class hierarchy Direct base class Inherited explicitly (one level up hierarchy) Indirect base class Inherited two or more levels up hierarchy Single inheritance Inherits from one base class Multiple inheritance Inherits from multiple base classes Base classes possibly unrelated

Three types of inheritance public Every object of derived class also object of base class Base-class objects not objects of derived classes Example: All cars vehicles, but not all vehicles cars Can access non- private members of base class Derived class can effect change to private base-class members Through inherited non- private member functions private Alternative to composition (covered later) protected Rarely used

Abstraction Focus on commonalities among objects in system is-a vs. has-a is-a Inheritance Derived class object treated as base class object Example: Car is a vehicle Vehicle properties/behaviors also car properties/behaviors has-a Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel

Base classes and derived classes Object of one class is an object of another class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral : base class Rectangle : derived class Base class typically represents larger set of objects than derived classes Example: Base class: Vehicle Cars, trucks, boats, bicycles, … Derived class: Car Smaller, more-specific subset of vehicles

Inheritance examples

Inheritance hierarchy Inheritance relationships: tree-like hierarchy structure Each class becomes Base class Supply data/behaviors to other classes OR Derived class Inherit data/behaviors from other classes

Single inheritance CommunityMember EmployeeStudent AdministratorTeacher AdministratorTeacher StaffFaculty Alumnus Single inheritance Multiple inheritance Inheritance hierarchy for university CommunityMembers.

Shape TwoDimensionalShapeThreeDimensionalShape CircleSquareTriangleSphereCubeTetrahedron Inheritance hierarchy for Shapes.

public inheritance Specify with: Class TwoDimensionalShape : public Shape Class TwoDimensionalShape inherits from class Shape Base class private members Not accessible directly Still inherited Manipulate through inherited member functions Base class public and protected members Inherited with original member access friend functions Not inherited

protected access Intermediate level of protection between public and private protected members accessible to Base class members Derived class members Derived-class members Refer to public and protected members of base class Simply use member names

Base class and derived class relationship Example: Point/circle inheritance hierarchy Point x-y coordinate pair Circle x-y coordinate pair Radius

Using protected data members Advantages Derived classes can modify values directly Slight increase in performance Avoid set/get function call overhead Disadvantages No validity checking Derived class can assign illegal value

Three level point/circle/cylinder hierarchy Point x-y coordinate pair Circle x-y coordinate pair Radius Cylinder x-y coordinate pair Radius Height

Instantiating derived-class object Chain of constructor calls Derived-class constructor invokes base class constructor Implicitly or explicitly Base of inheritance hierarchy Last constructor called in chain First constructor body to finish executing Example: Point3 / Circle4 / Cylinder hierarchy Point3 constructor called last Point3 constructor body finishes execution first Initializing data members Each base-class constructor initializes data members Inherited by derived class

Destroying derived-class object Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next Continue up hierarchy until final base reached After final base-class destructor, object removed from memory

Base-class constructors, destructors, assignment operators Not inherited by derived classes Derived class constructors, assignment operators can call Constructors Assignment operators

Uses a Object uses another object Call non- private member function Using pointer, reference or object name Knows a (association) Object aware of another object Contain pointer handle or reference handle Knowledge networks

Customizing existing software Inherit from existing classes Include additional members Redefine base-class members No direct access to base classs source code Link to object code Independent software vendors (ISVs) Develop proprietary code for sale/license Available in object-code format Users derive new classes Without accessing ISV proprietary source code