OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Inheritance.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved OOP and.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 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.
Chapter 12: Adding Functionality to Your Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
An Object-Oriented Approach to Programming Logic and Design
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Support for Object oriented Programming.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 9. OOP & ADTs: Introduction to Inheritance Read Chap. 12 A. Inheritance, OOD, and OOP (§12.1 & 12.2) A major objective of OOP: writing reusable code.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Inheritance ndex.html ndex.htmland “Java.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
1 9. OOP & ADTs: Introduction to Inheritance Read Chap. 12 A. Inheritance, OOD, and OOP (§12.1 & 12.2) A major objective of OOP: ______________________________.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
OOP What is problem? Solution? OOP
Object-Oriented Programming
Inheritance and Run time Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
More about OOP and ADTs Classes
Advanced C++ Topics Chapter 8.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Abstract Data Types and Encapsulation Concepts
Array Lists Chapter 6 Section 6.1 to 6.3
Object Based Programming
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance Basics Programming with Inheritance
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
More about OOP and ADTs Classes
Parameter Passing Actual vs formal parameters
Computer Programming with JAVA
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11: Inheritance and Composition
Chapter 8: Class Relationships
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
Chapter 11 Class Inheritance
Chapter 7 Inheritance.
Computer Science II for Majors
Presentation transcript:

OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Contents 14.1 A Brief History and Overview of OOP and ADTs 14.2 Inheritance and Object-Oriented Design 14.3 Building Derived Classes 14.4 Case Study: Payroll 14.5 Polymorphism, Virtual Functions, and ADTs 14.6 Case Study: A Heterogeneous Data Structure Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Objectives Study the basic properties of object-oriented programming: encapsulation, inheritance, an polymorphism Discuss object-oriented design and the role inheritance plays in it Describe how inheritance is implemented in C++ Look at examples of inheritance and class hierarchies Describe the need for polymorphism and how it is accomplished in C++ by means of virtual functions and late-binding Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

History, Overview Languages such as Pascal and C facilitated development of structured programs Need for ability to extend and reuse software became evident These lead to object-oriented programming First used in Smalltalk More widely available in C++ and Java Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Encapsulation Data and basic operations for processing this data are encapsulated into a single entity Made possible with introduction of Units Modules Libraries Packages Implementation details separated from definition Client code must use only public operations Implementation may be changed without affecting client code Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance A class can be derived from another class Example: New class inherits data and function members from original class Reusable for the new class Example: Consider need to add max() and min() functions to capability of a stack Could simply add these functions to the class But … alters already proven code Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance Adapter approach But … Build a new RevStack class Contains Stack object as its member But … We cannot say a RevStack is a Stack It only contains a Stack Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance Copy-&-paste approach Problem Build a RevStack class Copy, paste all Stack data members and function members in Add the max() and min() Problem RevStack and Stack are still separate and independent classes If we update Stack, we must change RevStack versions Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance Object-oriented approach Derive a new class, RevStack from Stack Stack is the base class or superclass RevStack is derived class or subclass Derived class inherits all members of base class Modifying Stack class automatically updates Revstack class Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Polymorphism and Dynamic Binding Previous observation of polymorphic behavior in functions and classes Function name can be overloaded Function template a pattern for multiple functions Class template a pattern for multiple classes In these cases the compiler determines which version of the function or class to use Called static or early binding Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Polymorphism and Dynamic Binding Consider the declaration of a stack pointer Stack * sptr; It can point to either a Stack or a RevStack If we give the command sptr->display(cout); Which code to use (for Stack or RevStack) cannot be determined until runtime This is dynamic or late binding Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance and Object-Oriented Design Simplified approach Identify objects in problem specs, their types Identify operations needed to solve the problem Arrange problem's objects, operations in sequence of steps – algorithm – to solve the problem Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inheritance and Object-Oriented Design Problems with simplified approach Operations not readily found, must create functions to accomplish Object types not available, must design new object types using classes In designing classes Identify all objects Group common objects into a base or general class Derive less-general classes from base class Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example: Licenses We need to model various licenses for a state bureau We study the variety and note groupings and subgroupings Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Base Class Begin at tree root, License class class License { public: /*** Function members ***/ void display (ostream & out) const; void read (istream & in); // … and other operations private: long myNumber; string myLastName, myFirstName; char myMiddleInitial; int myAge; Date myBirthDay; ... }; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

All derived from the License class Derived Classes All derived from the License class class DriversLicense : public License { public: ... private: int myVehicleType; string myRestrictionsCode; ... }; class HuntingLicense : public License { public: ... private: string thePrey; Date seasonBegin, seasonEnd; ... }; class PetLicense : public License { protected: ... private: string myAnimalType; ... }; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Declaration of a Derived Class Syntax DerivedClassName : kind BaseClassName { public: // new data members and private: // or protected // functions for derived class ... } kind is one of: - public - private - protected Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Fundamental Property of Derived Class Derive class inherits all members of base class And members of ancestor classes Cannot access private members of base class The kind of access it has to public and protected members depends on kind of inheritance Kind of inheritance Kind of access in derived class to public, protected members of base class public private protected public & protected, respectively Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Relationships Between Classes When class C2 is derived from class C1 Class C2 is-a C1 A HuntingLicense is-a License Use public inheritance only for is-a relationships Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Relationships Between Classes When class D1 contains a class D2 object as an element Then D1 has-a D2 A License has-a Date Public inheritance should not be used for has-a relationships Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Relationships Between Classes If class D1 needs information from class D2 Then D1 uses-a D2 An AutoInsurance class needs the name from a DriversLicense class AutoInsurance Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Building Derived Classes Derived class constructors Use parent class's constructors to initialize base class members Is actually a call to the base class constructor Called the member-initializer list Accessing inherited data members If base class data public, derived class can access, even alter it If base class data private, must use accessor functions If base class protected, may also alter it Author's preference is for private data, accessor functions Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Building Derived Classes Reusing Operations Derived class may extend or replace base class function of same name Possible to call the base class function with scope resolution operator void DerivedClass::whatever() { BaseClass::whatever(); . . . } Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example: Stacks and Bounded Stacks Consider need for a stack with limit on size Design BoundedStack class We need to ask Can we say a BoundedStack is-a Stack? If so, we can derive it from Stack May not be Needs altered push() method for full stack What does that do to inheritance? Note BoundedStack interface, Fig. 14.1 View use of this for conversion of numbers from base 10 to base 2, Fig. 14.2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Payroll Problem: Design Firm has variety of types of employees Pay calculated differently for each Design Look for commonality for all types of employees Define base class for common data members Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Payroll View base Employee Class, Fig 14.3A Derived class SalariedEmployee, Fig. 14.3B Derived Class HourlyEmployee, Fig. 14.3C Driver program to test base and derived classes, Fig. 14.3D Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Why Polymorphism Needed Note display() functions and overloaded output operator<< Declared, defined only for base class This works because of is-a relationship Note that only the Employee class data is displayed in this context We need other data to also be displayed The output operator<< function must be made polymorphic … it must know which version to use Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Virtual Functions, Dynamic Binding Which version is called must be deferred to run time Dynamic binding Accomplished with virtual functions Compiler creates a virtual function table (vtbl) for each object containing virtual functions Table of pointers to actual code for required function For our example we make display() a virtual function Note source code, Fig. 14.4A, driver program Fig. 14.4B Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example 1: Using Handles A handle is a variable whose value is the address of that object It is a pointer variable Refers to the object indirectly Handle for base class object Can also refer to any derived class object Employee * eptr; eptr = new Employee(); or eptr = new SaleriedEmployee(); Then eptr->display(cout); will always work Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example 2: Stack, BoundedStack Consider need for new operation, repeatedPush() to both Stack BoundedStack Possible solution Add function template to base class Stack Let BoundedStack inherit Note use of this approach, Fig 14.5 Note also (pg 837 of text) invalid output this strategy produces Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example 2: Stack, BoundedStack Best solution Make push() a virtual function in base class virtual void push (const ElementType & value); Result is that repeatedPush() for BoundedStack objects now uses correct version of push() Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Pure Virtual Functions, Abstract Classes Where no definition provided for one or more of function members These function members said to be pure virtual functions View source code for Document class Note use of keyword virtual and = 0 at end of declarations This makes them pure virtual functions Cannot declare instances of base class View source code of derived class, License Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Heterogeneous Data Structure Consider processing of a list empList of employees If we specify a list of handles (pointers) they can point to different derived types List<Employee *> empList; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Need for Virtual Functions Suppose our Employee base class has output member function display() Each descendant class also has its own display() Now when we traverse list and call empList[x]->display(cout); We need polymorphic behavior Same function call produces different results in different contexts Note application Header file for derived class Manager, Fig. 14.6A Driver program Fig. 14.6b Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3