Inheritance Virtual Functions, Dynamic Binding, and Polymorphism

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
Polymorphism, Virtual Methods and Abstract Classes
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Inheritance What is inheritance?
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Polymorphism.
Inheritance and Run time Polymorphism
CS250 Introduction to Computer Science II
Polymorphism Lec
Polymorphism Polymorphism
Testing with OO OO has several key concepts:
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheritance: Polymorphism and Virtual Functions
COP 3330 Object-oriented Programming in C++
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
Lecture 6: Polymorphism
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.
Computer Science II for Majors
Presentation transcript:

Inheritance Virtual Functions, Dynamic Binding, and Polymorphism Consider the following example: class B { public: void print(){ cout << “This is an object of type B”;} }; class D: public B { void print(){cout << “This is an object of type D”;} } B x; // instantiate object x D y; // instantiate object y B* ptr_B = &y; // make ptr_B point to y, is this possible? y.print(); // will this print “This is an object of type D” ? ptr_B->print(); // will this print “This is an object of type B” ? Can be used to develop a hierarchy of classes based on abstractions in a top down fashion, e.g. the Location-Point-Circle-Arch hierarchy introduced before It is also needed for code refinement, expandability, and reuse by a) Refining the implementation of a class by building a refined derived class, were some members functions are redefined, b) Expanding the functionality of the a class by adding new functions c) Reusing the code in a base class in several derived classes

Inheritance Virtual Functions , Dynamic Binding, and Polymorphism (cont.) A virtual function is a special member function invoked through a public base class reference or pointer, it is bound dynamically at run-time, class B { public: virtual void print(){ cout << “This is an object of type B”;}}; class D: public B {// is exactly the same as in the previous slide }; B x; // instantiate object x D y; // instantiate object y B* ptr_B = &y; // make ptr_B point to y y.print(); // will print “This is an object of type D” ptr_B->print(); // will also print “This is an object of type D” // The invocation of print() is bound to the object type not the pointer type

Inheritance Virtual Functions , Dynamic Binding, and Polymorphism (cont.) Dynamic binding is introduced using virtual functions The keyword virtual is a function specifier for member functions of a base class When a function at the base class class is declared virtual, all its redefined versions in the derived classes are bound dynamically according to the object type for which the function is invoked, not the pointer or reference type Polymorphism is a language mechanism that permits the same code expression to invoke different functions depending upon the type of object using the code, see the example in the next slide

Inheritance Polymorphism (cont.) Example: Consider the following example, suppose we have several classes derived from class B above class D1 : public B { public: void print();}; class D2 : public B { public: void print();}; …. Class Dn-1 : public B { public: void print();}; Also lets assume the existence of an array of pointers to objects as follows, B* objects_ptr[100]; // an array of pointers to objects of type B or // objects of type derived from B object_ptr[0] = new D, object_ptr[1] = new D1; object_ptr[2] = new D2; for(int j = 0 ; j < n ; j++) object_ptr[j]->print(); // different print() functions will be invoked // although we are iterating over the same code expression.

Inheritance Abstract Classes and Pure Virtual Functions An abstract class is a class for which no objects are created, it is only used as a base class in order to use virtual functions and dynamic binding for the objects of its derived classes, e.g. class Shape {// no objects of type Shape will be instantiated…}; class rectangle: public Shape {// this is a concrete class…..}; class Triangle: public Shape {// this is another concrete class….}; Shape* ptr = new rectangle; ptr = new Triangle; An abstract class must have at least one pure virtual function declared as follows, class Shape { public: virtual void rotate() = 0; // the equal 0 means the function // is a pure virtual function and it has no body code } The function rotate() only specifies a typical operation for the types of objects derived from Shape

Inheritance Abstract Classes and Pure Virtual Functions (cont.) An abstract class can not be used as an argument type or as a function return type, Shape a; // error, you can not instantiate objects of type Shape void fun(Shape x); // error void fun(Shape& x);// OK Shape fun1(void); // error Shape& fun1(void); // OK A reference or a pointer to Shape is of course allowed since they can be used to refer to an object of a class derived from Shape A derived class from an abstract base class also becomes abstract if the class does not redefine all the pure virtual functions defined at its base classes class X{ virtual void g() = 0; virtual void f() = 0; }; class Y : public X { void g(){// body code of g}// g is redefined // if f() is not redefined with code, class Y becomes also an abstract class