Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
ITEC200 – Week03 Inheritance and Class Hierarchies.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 4 Inheritance.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
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.
Chapter -6 Polymorphism
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Overview of C++ Polymorphism
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Object-Oriented Programming
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Overview of C++ Polymorphism
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
C++ Polymorphism Reference and pointer implicit type casting
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.
Presentation transcript:

Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor and Base Class Adding Members

Overriding a Method Methods can be override from the base class in the derived class by simply providing a derived class method with the same signature A derived class method must have the same or compatible return type Sometimes it is required to invoke the derived class method to the base class method to augment a base class method rather than doing something entirely different. This is known as partial overriding The scope operator can be used to call a base class method

Example class Workaholic : public Worker { public : void dowork( ) { Worker::doWork( ); // Work like a Worker drinkCof fee ( ) ; // Take a break Worker::doWork( ); // Work like a Worker some more } };

Static and Dynamic Binding Example Code 1 Worker w; 2 Workaholic wh; w. doWork ( ) ; wh. doWork ( ) ; The above code illustrates the fact that we can declare Worker and Workaholic objects in the same scope because the compiler can deduce which dowork method to apply w is a Worker and wh is a Workahol ic, so the determination of which dowork is used in the two calls at line 4 is computable at compile time The decision made at compile time about which function to use to resolve an overload static binding/overloading

Example 1 Worker *wptr; 2 cin >> x; 3 if (x != 0 ) 4 wptr =new Workaholic ( ); 5 else 6 wptr= new Worker ( ); 7 8 …… 9 wptr -> doWork ( ); //which doWork is used

Explanation of Example If x is zero, we create a Worker object; otherwise, we create a Workaholic object Recall that, as a Workaholic IS-A Worker, a Workaholic can be accessed by a pointer to a Worker Any method that we might call for Worker will have a meaning for Workaholic objects Public inheritance automatically defines a type conversion from a pointer to a derived class to a pointer to the base class We can declare that wptr is a pointer to the base class Worker and then dynamically allocate either a Worker or Workaholicobject for it to point at

Continued…. The decision about which dowork to use can be made at compile time or at run time If the decision is made at compile time, worker's dowork must be used because that is the type of *wptr at compile time If wptr is actually pointing at Workaholic, this decision is wrong. Because the type of object that wptr is actually pointing at can be determined only as the program runs, this decision must be made at run time A run-time decision to apply the method corresponding to the actual referenced object is called dynamic binding It is almost always the preferred course of action

Continued…. However a run-time decision incurs some run-time overhead because it requires that the program maintain extra information and that the compiler generate code to perform the test This overhead was once thought to be significant Although other languages such as Smalltalk and Objective C use dynamic binding by default, C++ does not

Virtual Function A virtual function uses dynamic binding if a compile-time binding decision is impossible to make A nonvirtual function will always use static binding The default is that functions are nonvirtual. This condition is unfortunate because we now know that the overhead is relatively minor As a result, a nonvirtual function should be used only when the function is invariant over the inheritance hierarchy

Continued…. Virtualness is inherited, so it can be indicated in the base class Thus if the base class declares that a function is virtual the decision can be made at run time; otherwise, it is made at compile time For example In the exception class, the what method is virtual. The derived classes require no further action to have dynamic binding apply for what method calls

Continued…. In last example code of Work class, doWork is called if it is declared as virtual If dowork is not virtual in the Worker class, but is later made virtual in Workaholic, then accesses through pointers and references to Worker will still use static binding To make a run-time decision, the keyword virtual have to place at the start of the dowork declaration in the Worker class interface as class Worker { public : virtual void dowork( ); };

Summarizing Topic Static binding is used by default Dynamic binding is used for virtual functions if the binding cannot be resolved at compile time However, a run-time decision is needed only when an object is accessed through a pointer or reference to a base class

Types of Member Function Three types of member function Non-virtual function Virtual function Pure virtual function

Non-virtual Function Overloading is resolved at compile time To ensure consistency when pointers to objects are used We generally use a non-virtual method only when the function is invariant over the inheritance hierarchy Constructors are always non-virtual

Virtual Function Overloading is resolved at run time The base class provides a default implementation that may be overridden by the derived classes Destructors should be virtual

Pure Virtual Function Overloading is resolved at run time The base class provides no implementation and is abstract The absence of a default requires either that the derived classes provide an implementation or that the derived classes themselves be abstract

Abstract Method An abstract method is declared in the base class It always defined in the derived class It says (in the base class), what all class objects in the hierarchy can do and must eventually implement It does not provide a default implementation, so each derived class must provide its own implementation

Abstract Class A class that has at least one abstract method is called an abstract class The behavior of an abstract class is not completely defined, that’s why abstract classes can never be instantiated When a derived class fails to override an abstract method with an implementation, the method remains abstract in the derived class As a result, the derived class remains abstract, and the compiler reports an error if an attempt to instantiate the abstract derived class is made

Constructor and Destructor Virtual or Not-Virtual? Constructors are never virtual Destructors should always be made virtual if they are being used in a base class and should be non-virtual otherwise

Continued…. For constructors a virtual label is meaningless We can always determine at compile time what we are constructing As a result, declaring a constructor virtual is forbidden by the Standard Destructors need to be virtual to ensure that the destructor for the actual object is called If the derived class consists of some additional members that have dynamically allocated memory, that memory will not be freed by the base class destructor The destructor is no different from any other member function

Continued…. Even if the default destructor seems to work, it actually does not work if there is inheritance In an inheritance hierarchy, then, the base class constructor should always be virtual Even if it is a trivial destructor, it should be written anyway, with a virtual declaration and empty body When the destructor is virtual, we are certain that a run- time decision will be used to choose the destructor appropriate to the object being deleted

Common Errors Inheritance is private by default. A common error is to omit the keyword public, which is needed to specify public inheritance. If a base class member function is redefined in a derived class, it should be made virtual. Otherwise, the wrong function could be called when accessed through a pointer or reference. Base class destructors should be declared as virtual functions. Otherwise, the wrong destructor may get called in some cases.

Continued…. Constructors can never be declared virtual. Objects of an abstract base class cannot be instantiated. If the derived class fails to implement any inherited pure virtual function, the derived class becomes abstract and cannot be instantiated, even if it makes no attempts to use the undefined pure virtual function. Never redefine a default parameter for a virtual function. Default parameters are bound at compile time, which can create an inconsistency with virtual functions that are bound at run time. To access a base class member, the scope resolution must be used. Otherwise, the scope is the current class.

Continued…. In a derived class, the inherited base class members can be initialized only as an aggregate in a constructor's initializer list. If these members are public or protected, they may later be read or assigned to individually. If a constructor declaration is provided in the base class, provide the definition, too, for the same reason as in the destructor case.

Summary of Chapter Inheritance is a powerful feature that allows the reuse of code. However, be sure that functions applied to objects created at run time through the new operator are bound at run time. This feature is known as dynamic binding, and the use of virtual functions is required to ensure that run-time decisions are made. Reread this chapter as often as necessary to ensure that you understand the distinctions among non-virtual functions virtual functions pure virtual functions