Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

Slides:



Advertisements
Similar presentations
Has-a Relationships A pen “has a” or “contains a” ball.
Advertisements

OOP: Inheritance By: Lamiaa Said.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
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
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance in the Java programming language J. W. Rider.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object-Oriented Programming: Inheritance and Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Interlude 1 C++ Classes CS Data Structures Mehmet H Gunes Modified from authors’ slides.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
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: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Java Topics Chapter 9
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
CS212: Object Oriented Analysis and Design
Types of Programming Languages
Advanced C++ Topics Chapter 8.
C++ Classes C++ Interlude 1
C++ Classes C++ Interlude 1.
Object Oriented Programming
Advanced Java Topics Chapter 9 (continued)
Interfaces.
Figure 8.1 Inheritance: Relationships among timepieces.
Abstract Classes AKEEL AHMED.
Advanced Java Topics Chapter 9
Polymorphism Polymorphism
Sorted Lists and Their Implementations
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
CIS 199 Final Review.
Lecture 10 Concepts of Programming Languages
Chapter 11 Class Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Contents Inheritance Revisited Containment: Has-a Relationships Abstract Base Classes Revisited Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © __/aw/streaming/ecs_carrano_dapscpp_6/CI4_C_Inheritance_and_Polymorphism.m4v

Inheritance Revisited Inheritance: Relationships among timepieces Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited Multiple inheritance Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited The class PlainBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited Derived class MagicBox inherits members of base class PlainBox, redefines and adds members Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 New Redefined

Inheritance Revisited The class MagicBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited Instance of derived class has all behaviors of its base class Derived class inherits private members from base class – But cannot access them directly Derived class’s methods can call base class’s public methods Clients of a derived class can invoke base class’s public methods Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited Early, or static, binding: Compiler determines which version of a method to invoke Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Inheritance Revisited Late binding: appropriate version of method decided at execution time Polymorphic method has multiple meanings Virtual method: can be overridden Method virtual in base class, virtual in any derived class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Public, Private, and Protected Sections of a Class Virtual method tables when cardBoxPtr points to (a) an instance of PlainBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Public, Private, and Protected Sections of a Class Access to public, private, and protected sections of a class by a client and a derived class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Public, Private, and Protected Sections of a Class Virtual method tables when cardBoxPtr points to (b) an instance of MagicBox Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Access Categories of a Class Public members can be used by anyone. Private members can be used only by methods of the class. Guideline: In general, class’s data members should be private Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Kinds of Inheritance Public inheritance – Public and protected members of base class remain, public and protected members of derived class Protected inheritance – Public and protected members of base class are protected members of derived class. Private inheritance – Public and protected members of base class are private members of the derived class. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Is-a and As-a Relationships A magic box is a plain box Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Is-a and As-a Relationships Use public inheritance only when is-a relationship exists between two classes You can use instance of derived class – anywhere you can use instance of base class If relationship between 2 classes not is-a, – should not use public inheritance If class needs access to protected members of another class or if you need to redefine methods in that class, – You can form an as-a relationship Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Containment: Has-a Relationships A pen has a or contains a ball Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Execution Order of Constructors and Destructors Constructors execute in following order: 1.Its base class constructor executes. 2.Constructors of its member objects execute in the declaration order. 3.The body of its constructor executes. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Execution Order of Constructors and Destructors Destructors execute in the opposite order: 1.The body of its destructor executes. 2.Destructors of its member objects execute in the declaration order. 3.Its base class destructor executes. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited CDP and DVDP have an abstract base class Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited CDP and DVDP are derived from GDP Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited A class that contains at least one pure virtual method is an abstract base class An abstract base class has descendants but no instance Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited An abstract class that is an interface for the ADT box Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited UML class diagram of the family of box classes Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited By definition is a class that contains at least one pure virtual method. Used only as the basis for derived classes – Thus defines a minimum interface for its descendants. Has no instances. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Abstract Base Classes Revisited Should, in general, omit implementations – Except for the destructor methods that provide access to private data members. That is, virtual methods in an abstract base class usually should be pure. Must implement any virtual method that is not pure, – Thereby providing a default implementation if derived class chooses not to supply its own. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012