Controlling Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance Modes When deriving.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Overview of Data Structures and Algorithms
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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,
Inheritance1 Inheritance Software re-use with derived classes.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CMSC 202 Inheritance.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Coming up: Inheritance
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Binary Trees Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Trees A binary tree is either.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Polymorphism Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Definition polymorphismthe ability to.
Composition OO Software Design and Construction Computer Science Dept Va Tech January 2000 ©2000 McQuain WD 1 Composition: Cooperating Classes Composition.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Review Array Array Elements Accessing array elements
Chapter 4 The easy stuff.
CSCI-255 LinkedList.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Review: Two Programming Paradigms
Stacks and Queues.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Understanding Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Doubly Linked List Implementation
Visit for more Learning Resources
Java Programming Language
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CS410 – Software Engineering Lecture #8: Advanced C++ III
Doubly Linked List Implementation
Computer Science II for Majors
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Controlling Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance Modes When deriving a class D from a base class B, we may specify that access to the base class is any of the following: public, protected, private. The base class access specifier controls the access derived types will have to base members and the conversion of a pointer to the derived type to a pointer to the base type. public -public members of B become public in D -protected members of B become protected members of D, accessible only to members and friends of D and of classes derived from D -private members of B are inaccessible to D -any function can convert a D* to a B* The most common specification is:

Controlling Inheritance Data Structures & OO Development I 2 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance Modes Access privileges for the other access specifiers: protected -public and protected members of B become protected members of D, accessible only to members and friends of D and of classes derived from D -private members of B are inaccessible to D -only members and friends of D, and of classes derived from D, function can convert a D* to a B* private -public and protected members of B become private members of D, accessible only to members and friends of D, but NOT to classes derived from D -private members of B are inaccessible to D - only members and friends of D can convert a D* to a B*

Controlling Inheritance Data Structures & OO Development I 3 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Using Non-Public Inheritance Private inheritance is appropriate when the public interface of the base class is not needed by the user of the derived class, or if it is desirable to hide the public interface of the base class from the user. Of course, this will also render any protected members of the base class inaccessible to classes derived from the derived class. For that reason private inheritance is used much less often than public inheritance. Similarly, protected inheritance is appropriate when the public interface of the base class must be hidden from the user of the derived class, but the protected and public interface of the base class is useful in the implementation of classes derived from the derived class.

Controlling Inheritance Data Structures & OO Development I 4 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Private Inheritance Example Consider implementing a stack class, given that there is a tested, reliable linked list class available: class LList { private: Node* Head, *Curr, *Tail; public: LList(); bool Prefix(const Item& Data); // insert at front of list bool Append(const Item& Data); // insert at tail of list Item delFirst(); // delete front node Item delLast(); // delete tail node... bool isEmpty() const; bool isFull() const; ~LList(); }; The list class has all the functionality we need, and then some… we need to hide the dangerous parts of the list interface…

Controlling Inheritance Data Structures & OO Development I 5 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Private Inheritance Example class Stack : private LList { public: Stack(); Item Pop(); bool Push(const Item& Data);... ~Stack(); }; By deriving the Stack class using private inheritance, we gain the capabilities of the list class but can hide the list interface behind an appropriate interface: Item Stack::Pop() { return delFirst(); }; The Stack class interface just serves as a "front" for the broader List interface, which is entirely hidden from the user. Classes derived from Stack cannot access the "inappropriate" List interface either… it's completely buried.

Controlling Inheritance Data Structures & OO Development I 6 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Protected Inheritance Example class Polynomial : protected QueueT { private: //... public: Polynomial(); //... Polynomial operator+(Polynomial& RHS); ~Polynomial(); }; A Polynomial class could be derived from an instantiation of the queue template QueueT seen earlier: The queue is used to hold the coefficients of the polynomial, in order, with zeros stored for missing terms… straightforward and it corresponds nicely to the way most polynomial manipulations work. We could use private inheritance here, but a derived type (such as QuadraticPolynomial ) would be seriously inconvenienced.

Controlling Inheritance Data Structures & OO Development I 7 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Summary private and protected inheritance access rules are somewhat complex. It may be difficult to keep track of all the implications within a large hierarchy. Aggregation is often, but not always, a more natural way to deal with the problems that motivated using a private or protected base class.

Controlling Inheritance Data Structures & OO Development I 8 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Hiding Inherited Methods Sometimes a member function from the base type simply doesn’t make sense within the context of a derived type. What do we do? class Rectangle { private: Location NW; int Length, Width; public: //... void ReScale(int Factor) {Length = Factor*Length; Width = Factor*Width;} void ReSize(int L, int W) {Length = L; Width = W;} //... }; class Square : public Rectangle { public: //... }; We don’t want to allow a Square to not have Length == Width How to prevent that…?

Controlling Inheritance Data Structures & OO Development I 9 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Handling an Embarrassing Base Method There are three strategies: 1. Override the base member function so it’s harmless. 2. Use private inheritance so the base method isn’t visible to the user of the derived class. 3. Revise the inheritance hierarchy to make it more appropriate. Let’s look at all three...

Controlling Inheritance Data Structures & OO Development I 10 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Overriding an Embarrassing Base Method What are the pros and cons for this solution? void Square::ReSize(int L, int W) { if (L == W) { Length = L; Width = W; } void Square::ReSize(int L, int W) { Rectangle::ReSize(L, L); } or

Controlling Inheritance Data Structures & OO Development I 11 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Use Private Inheritance This will render Rectangle::ReSize() invisible to the user who declares an object of type Square. That eliminates any chance the user could incorrectly use the inappropriate base class member function. What are the pros and cons for this solution? class Square : Rectangle { // default mode is private public: //... };

Controlling Inheritance Data Structures & OO Development I 12 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Revise Inheritance Hierarchy It doesn’t really make sense to say that a square is a rectangle (HS geometry books notwithstanding) … However, it DOES make sense to say that squares and rectangles are kinds of quadrilaterals: Quadrilateral ?? Square Scale Rectangle Scale ReSize

Controlling Inheritance Data Structures & OO Development I 13 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Summary If the base class has a member function that the derived class needs to extend or modify, that can be done simply by overriding. This doesn't necessarily indicate a problem with the design of the hierarchy. If the base class has a member function that the derived class needs to hide from users, that is usually an indication that the base type hasn't been chosen carefully enough. The "inappropriate base member" problem is often best solved by revising the inheritance hierarchy. However, that can lead to another problem: cluttermorphism.

Controlling Inheritance Data Structures & OO Development I 14 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance and Development Inheritance provides a number of benefits with respect to development: -reusability of common implementation -representation of natural logical relationships among types Inheritance also carries a cost: -designing modifications to base class require understanding the effect on all derived classes -designing modifications to derived class requires understanding of the relationship to the base class (not usually too serious) -modifications to base class will require re-testing implementations of derived classes to verify nothing is broken