Inheritance Modes. Controlling Inheritance 1

Similar documents
Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Short Notes of CS201

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CS201 - Introduction to Programming Glossary By

And Even More and More C++ Fundamentals of Computer Science

clean way to do separate compilation. ECS140A Programming Languages 12-1

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Instantiation of Template class

Object-Oriented Principles and Practice / C++

ECE 3574: Dynamic Polymorphism using Inheritance

Chapter 1: Object-Oriented Programming Using C++

Inheritance, and Polymorphism.

CS 520 Theory and Practice of Software Engineering Fall 2017

Interfaces & Generics

C++ Inheritance and Encapsulation

Chapter 6. Object- Oriented Programming Part II

More C++ : Vectors, Classes, Inheritance, Templates

September 19,

Queue Definition. Ordered list with property:

September 10,

Object-Oriented Principles and Practice / C++

Object-Oriented Programming for Scientific Computing

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Fast Introduction to Object Oriented Programming and C++

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

7.1 Optional Parameters

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Motivation for Templates

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Functions BCA-105. Few Facts About Functions:

C++ Templates. David Camp

Cpt S 122 Data Structures. Templatized Stack

CS 320 Introduction to Software Engineering Spring March 06, 2017

CS11 Introduction to C++ Fall Lecture 7

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

COEN244: Class & function templates

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

C++ Important Questions with Answers

An Introduction to C++

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 11. Abstract Data Types and Encapsulation Concepts

CS 320 Introduction to Software Engineering Spring March

CPSC 427a: Object-Oriented Programming

Class and Function Templates

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

What is Class? Remember

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

D Programming Language

CPSC 427: Object-Oriented Programming

G52CPP C++ Programming Lecture 13

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Object Oriented Programming in C#

CPSC 427: Object-Oriented Programming

Absolute C++ Walter Savitch

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Data Structures and Algorithms(2)

Introduction to Object-Oriented Programming

What are the characteristics of Object Oriented programming language?

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Today s lecture. CS 314 fall 01 C++ 1, page 1

Data Structures (INE2011)

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Increases Program Structure which results in greater reliability. Polymorphism

CS/ENGRD 2110 SPRING 2018

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Introduction to Object-Oriented Programming

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

An Introduction to Queues With Examples in C++

Networked Embedded System Patterns for C Developers. Overview of C (& C++) Programming Styles. Douglas C. Schmidt

OBJECT ORIENTED PROGRAMMING USING C++

16 Multiple Inheritance and Extending ADTs

Lesson 13 - Vectors Dynamic Data Storage

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Memory and Pointers written by Cathy Saxton

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

CSI33 Data Structures

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CS 247: Software Engineering Principles. Design Patterns

Objects and Classes. Engineering 1D04, Teaching Session 9

VALLIAMMAI ENGINEERING COLLEGE

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

CS 520 Theory and Practice of Software Engineering Fall 2018

Item 4: Extensible Templates: Via Inheritance or Traits?

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Transcription:

Inheritance Modes Controlling Inheritance 1 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. The most common specification is: 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*

Inheritance Modes Controlling Inheritance 2 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*

Using Non-Public Inheritance Controlling Inheritance 3 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.

Private Inheritance Example Controlling Inheritance 4 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

Private Inheritance Example Controlling Inheritance 5 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: class Stack : private LList { public: Stack(); Item Pop(); bool Push(const Item& Data);... Item Stack::Pop() { ~Stack(); 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.

Protected Inheritance Example Controlling Inheritance 6 A Polynomial class could be derived from an instantiation of the queue template QueueT seen earlier: class Polynomial : protected QueueT<double> { private: //... public: Polynomial(); //... Polynomial operator+(polynomial& RHS); ~Polynomial(); }; 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.

Summary Controlling Inheritance 7 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.

Hiding Inherited Methods Controlling Inheritance 8 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;} //... }; We don t want to allow a Square to not have Length == Width How to prevent that? class Square : public Rectangle { public: //... };

Handling an Embarrassing Base Method Controlling Inheritance 9 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...

Overriding an Embarrassing Base Method Controlling Inheritance 10 void Square::ReSize(int L, int W) { } if (L == W) { Length = L; Width = W; } or void Square::ReSize(int L, int W) { } Rectangle::ReSize(L, L); What are the pros and cons for this solution?

Use Private Inheritance Controlling Inheritance 11 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: //... };

Revise Inheritance Hierarchy Controlling Inheritance 12 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

Summary Controlling Inheritance 13 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.

Inheritance and Development Controlling Inheritance 14 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