Overview of C++ Polymorphism

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.
Inheritance Inheritance Reserved word protected Reserved word super
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
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,
© 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.
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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++ 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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
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 Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Goals for today’s review
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 Inheritance  Protected Members  Non-public Inheritance  Virtual Function Implementation  Virtual Destructors  Abstract Base Classes and Interfaces.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 3370 – C++ Object-oriented Programming
Polymorphism &Virtual Functions
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Overloading
Programming II Polymorphism A.AlOsaimi.
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
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.
Presentation transcript:

Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined “User” defined types: declared classes, structs, unions including types provided by the C++ standard libraries Native types are “built in” to the C++ language itself: int, long, float, … A typedef creates a new type name for another type (type aliasing) Public inheritance creates sub-types Inheritance only applies to user-defined classes (and structs) A publicly derived class is-a subtype of its base class Known as “inheritance polymorphism” Template parameters also induce a subtype relation Known as “interface polymorphism” We’ll cover how this works in depth, in later sessions Liskov Substitution Principle (for both kinds of polymorphism) if S is a subtype of T, then wherever you need a T you can use an S

C++ Polymorphism, Continued Inheritance polymorphism depends on public virtual member functions in C++ Base class declares a member function virtual Derived class overrides the base class’s definition of the function Private or protected inheritance creates a form of encapsulation Does not create a substitutable sub-type A privately derived class wraps its base class The class form of the Adapter Pattern uses this technique

Static vs. Dynamic Type The type of a variable is known statically (at compile time), based on its declaration int i; int * p; Fish f; Mammal m; Fish * fp = &f; However, actual types of objects aliased by references & pointers to base classes vary dynamically (at run-time) Animal * ap = &f; ap = &m; Animal & ar = get_animal(); Animal Fish Mammal A base class and its derived classes form a set of types type(*ap)  {Animal, Fish, Mammal} typeset(*fp)  typeset(*ap) Each type set is open More subclasses can be added

Derived class inherits from base class Public Inheritance (“is a”) Forms of Inheritance Derived class inherits from base class Public Inheritance (“is a”) Public part of base class remains public Protected part of base class remains protected Protected Inheritance (“contains a”) Public part of base class becomes protected Private Inheritance (“contains a”) Public part of base class becomes private Protected part of base class becomes private

Public, Protected, Private Inheritance class A { public: int i; protected: int j; private: int k; }; Class B : public A { // ... Class C : protected A { Class D : private A { Class A declares 3 variables i is public to all users of class A j is protected. It can only be used by methods in class A or its derived classes (+ friends) k is private. It can only be used by methods in class A (+ friends) Class B uses public inheritance from A i remains public to all users of class B j remains protected. It can be used by methods in class B or its derived classes Class C uses protected inheritance from A i becomes protected in C, so the only users of class C that can access i are the methods of class C j remains protected. It can be used by methods in class C or its derived classes Class D uses private inheritance from A i and j become private in D, so only methods of class D can access them.

Class and Member Construction Order public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) : A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} int m_j; int main (int, char *[]) { B b(2,3); return 0; In the main function, the B constructor is called on object b Passes in integer values 2 and 3 B constructor calls A constructor passes value 2 to A constructor via base/member initialization list A constructor initializes m_i with the passed value 2 Body of A constructor runs Outputs “A” B constructor initializes m_j with passed value 3 Body of B constructor runs outputs “B”

Class and Member Destruction Order public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} int m_j; int main (int, char *[]) { B b(2,3); return 0; B destructor called on object b in main Body of B destructor runs outputs “~B” B destructor calls “destructor” of m_j int is a built-in type, so it’s a no-op B destructor calls A destructor Body of A destructor runs outputs “~A” A destructor calls “destructor” of m_i again a no-op Compare orders of construction and destruction of base, members, body at the level of each class, order of steps is reversed in constructor vs. destructor ctor: base class, members, body dtor: body, members, base class

Virtual Functions class A { public: A () {cout<<" A";} virtual ~A () {cout<<" ~A";} virtual f(int); }; class B : public A { B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} virtual f(int) override; //C++11 int main (int, char *[]) { // prints "A B" A *ap = new B; // prints "~B ~A" : would only // print "~A" if non-virtual delete ap; return 0; Used to support polymorphism with pointers and references Declared virtual in a base class Can overridde in derived class Overriding only happens when signatures are the same Otherwise it just overloads the function or operator name More about overloading next lecture Ensures derived class function definition is resolved dynamically E.g., that destructors farther down the hierarchy get called Use final (C++11) to prevent overriding of a virtual method Use override (C++11) in derived class to ensure that the signatures match (error if not)

Virtual Functions Only matter with pointer or reference class A { public: void x() {cout<<"A::x";}; virtual void y() {cout<<"A::y";}; }; class B : public A { void x() {cout<<"B::x";}; virtual void y() {cout<<"B::y";}; int main () { B b; A *ap = &b; B *bp = &b; b.x (); // prints "B::x" b.y (); // prints "B::y" bp->x (); // prints "B::x" bp->y (); // prints "B::y" ap->x (); // prints "A::x" ap->y (); // prints "B::y" return 0; Only matter with pointer or reference Calls on object itself resolved statically E.g., b.y(); Look first at pointer/reference type If non-virtual there, resolve statically E.g., ap->x(); If virtual there, resolve dynamically E.g., ap->y(); Note that virtual keyword need not be repeated in derived classes But it’s good style to do so Caller can force static resolution of a virtual function via scope operator E.g., ap->A::y(); prints “A::y”

Potential Problem: Class Slicing Catch derived exception types by reference Also pass derived types by reference Otherwise a temporary variable is created Loses original exception’s “dynamic type” Results in “the class slicing problem” where only the base class parts and not derived class parts copy

Pure Virtual Functions class A { public: virtual void x() = 0; virtual void y() = 0; }; class B : public A { virtual void x(); class C : public B { virtual void y(); int main () { A * ap = new C; ap->x (); ap->y (); delete ap; return 0; A is an abstract (base) class Similar to an interface in Java Declares pure virtual functions (=0) May also have non-virtual methods, as well as virtual methods that are not pure virtual Derived classes override pure virtual methods B overrides x(), C overrides y() Can’t instantiate an abstract class class that declares pure virtual functions or inherits ones that are not overridden A and B are abstract, can create a C Can still have a pointer or reference to an abstract class type Useful for polymorphism

Design with Pure Virtual Functions Pure virtual functions let us specify interfaces appropriately But let us defer implementation decisions until later (subclasses) As the type hierarchy is extended, pure virtual functions are replaced By virtual functions that fill in (and may override) the implementation details Key idea: refinement Animal move()=0 Fish Mammal move() move() swim() walk() Bird Sparrow Penguin move() move() walk() waddle() fly() swim()

Summary: Tips on Inheritance Polymorphism A key tension Push common code and variables up into base classes Make base classes as general as possible Use abstract base classes to declare interfaces Use public inheritance to make sets of polymorphic types Use private or protected inheritance only for encapsulation Inheritance polymorphism depends on dynamic typing Use a base-class pointer (or reference) if you want inheritance polymorphism of the objects pointed to (or referenced) Use virtual member functions for dynamic overriding Even though you don’t have to, label each inherited virtual (and pure virtual) method “virtual” in derived classes Use final (C++11) to prevent overriding of a virtual method Use override (C++11) to make sure signatures match