Inheritance1 Inheritance Software re-use with derived classes.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
Inheritance Inheritance Reserved word protected Reserved word super
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Lecture 9 Concepts of Programming Languages
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Class List { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // Or: static const size_t CAPACITY = 30; typedef double Item; // CONSTRUCTOR.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object-Oriented Programming: Inheritance and Polymorphism.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Unit 2 - Stack and Queue Slides 18 to end. Queue CMPT Anne Lavergne18.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Chapter 22 - C++ Templates
Homework 4 questions???.
C++ Plus Data Structures
An Introduction to Inheritance
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Array-Based Implementations
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming: Inheritance and Polymorphism
Chapter 8: Class Relationships
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
Presentation transcript:

inheritance1 Inheritance Software re-use with derived classes

inheritance2 Inheritance Object-oriented languages allow new classes to acquire properties from an existing class - this is known as inheritance Original class is called the base or parent class, or the superclass The new, slightly different class is the derived or child class, or the subclass

inheritance3 Derived classes A derived class contains all members and functions of the base class The subclass inherits these members, along with their access characteristics The declaration of the child class determines how these access characteristics are manifested in the child class

inheritance4 Derived class declaration In the declaration of the child class, the parent class is listed as either public or private; for example: class Set : public Bag –With this declaration, public members of Bag are inherited as public members of Set –private members are inherited, but can be accessed only through inherited functions

inheritance5 Derived class declaration A derived class can also be declared with a private base class: class Set : private Bag –public members of Bag are inherited as private members of Set –private members of Bag are accessible only through Bag member functions

inheritance6 Protected class members If a base class member is declared as protected, rather than public or private, that member is directly accessible by any class derived from the base Protected members act just like private members in terms of access by non-member functions, except for members of derived classes

inheritance7 Example of inheritance in action We will define two classes, Point and Circle Circle inherits Point members and adds members of its own The base class (Point) data members are declared protected, rather than private, enabling direct access by the derived class (Circle)

inheritance8 Base Class Declaration class Point { public: Point (double a=0.0, double b=0.0); void setPoint(double a, double b); double getX()const {return x;} double getY()const {return y;} friend ostream& operator << (ostream &o, const Point &p); protected: double x; double y; };

inheritance9 Base class function definitions Point::Point (double a, double b)void Point::setPoint(double a, double b){x = a;y = b;} ostream & operator << (ostream &o, const Point &p) { o << “[” << p.x << “, ” << p.y << “]”; return o; }

inheritance10 Child class definition class Circle : public Point { public: Circle (double r =0.0, double x=0, double y=0); void setRadius(double r) {radius = r;} double getRadius () const {return radius;} double area () const {return radius*radius* ;} friend ostream& operator << (ostream &o, const Circle &c); protected: double radius; };

inheritance11 Circle constructor Circle::Circle(double r, double x, double y) : Point(x,y) { radius = r; } The line preceding the function’s begin bracket :Point (x,y) indicates a call to the constructor of the parent class This is not actually a separate line of code, but part of the function’s heading

inheritance12 Circle friend function ostream& operator << (ostream &o, const Circle &c) { o << “Center = [” << c.x << “, ” << c.y << “]” << “ Radius = ” << c.radius; return o; } Members x and y of parent class Point are accessible to child class Circle because of protected status Members x and y are members of the Circle object; in other words, there is a Point in every Circle

inheritance13 The “is-a” relationship The Point/Circle example is atypical of the way inheritance is usually used Inheritance usually involves creating a generic base class, then deriving several child classes from it The relationship of the child to the parent is usually described using the phrase “is a” -- we say a derived class object is a parent class object

inheritance14 The “is-a” relationship For example, your textbook describes a generic Clock class, then creates the derived class CuckooClock It is natural to say “a CuckooClock is a Clock” - meaning a cuckoo clock is a kind of clock It wouldn’t be too much of a stretch to derive other Clock types - alarm clock, 24- hour clock, etc.

inheritance15 Using a derived class Derived class objects can call base class member functions as if they were member functions of the derived class An object of the derived class may be used in place of a base class object - for example, if a function takes a base class object as a parameter, a derived class object may be substituted

inheritance16 Derived classes & assignment Assignments are allowed form a derived class to a base class if the base class is public However, a base class object cannot be used as if it were an object of the derived class - so you can’t assign a base class object to a derived class variable

inheritance17 Inheritance & Overriding A derived class inherits all members of the base class, but often a derived object needs to perform a particular operation a different way than that defined in an inherited function When a new function body is provided for an inherited member function within a derived class, the function is overridden

inheritance18 Inheritance & Overriding Sometimes overriding a function involves adding functionality, rather than simply replacing an original base class function When this is the case, the overriding function can call the base class function as its first action, then proceed with additional operations

inheritance19 Example int Clock24::get_hour() const // overrides inherited function { int ohr; ohr = Clock::get_hour() // call to overridden function if (is_morning()) { if (ohr == 12) return 0; else return ohr; } etc.

inheritance20 Using inheritance with ADTs Throughout the semester, we have studied various container classes In many ways, one container is very much like another; inheritance can be used to derive many different types of containers by placing common characteristics in a base class

inheritance21 Example: Set derived from Bag Both Bags and Sets contain unordered collection of elements The basic difference between them is that all elements in a Set must be unique We can create a Set class by inheriting from Bag, and overriding the insert function to eliminate insertion of duplicates

inheritance22 Example: Generic List Class Suppose you are provided with a generic ADT to hold an ordered list of items The data part of the List class is unknown to you, but you are given a set of member functions and a description of their actions, as shown on the slides that follow

inheritance23 List member functions List() // postcondition: initializes an empty List bool is_item() const // postcondition: returns true if there is a valid current // item; false if there is no valid current item Item current() const // precondition: is_item returns true // postcondition: returns current List item void start() // postconditon: first item on List becomes current item; // if list is empty, there is no current item

inheritance24 List member functions void advance() // precondition: is_item returns true // postcondition: if current item was the last on List, there is // now no current item; otherwise, the new current item is the // one immediately following the original current item void insert (const Item &entry) // postcondition: a new copy of entry has been inserted before // the current item; if no current item existed, entry was inserted // at the front of the List. Item containing entry is now the // current item

inheritance25 List member functions void attach (const Item &entry) // postcondition: a new copy of enty has been inserted in list // after the current item; if there was no current item, entry was // inserted at the end of the list. Item containing entry is now the // current item void remove_current() // precondition: is_item returns true // postcondition: current item has been removed; item following // current (if one existed) is now current item size_t size() const // postcondition: returns count of items in List

inheritance26 Deriving Stack from List Stack is defined by its push and pop functions, respectively inserting and removing items from the same end of the structure Because List has many functions that won’t (and shouldn’t) be used directly by users of Stack code, List is inherited as a private base class

inheritance27 Stack class definition template class Stack : private List { public: void push(const Item &entry); Item pop(); Item peek() const; List ::size; // changes inherited private function size to public // Stack function available to Stack user - this is an // example of exempting a member from private base // class bool is_empty() const {return size() == 0;} };

inheritance28 Stack member functions template void push (const Item &entry) { insert(entry); } template Item pop() { Item answer; assert(size() > 0); answer = current(); remove_current(); return answer; } template Item peek() { assert (size() > 0); return current(); }

inheritance29 Deriving Queue from List A child queue class could be derived from List in a similar fashion List would be inherited as a private base class, and queue functions enqueue and dequeue would be defined using inherited private functions from List

inheritance30 Inheritance Software re-use with derived classes - ends -