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.

Slides:



Advertisements
Similar presentations
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Advertisements

C++ Classes & Data Abstraction
LECTURE LECTURE 17 More on Inheritance, Virtual Inheritance, & Virtual Destructors, 17.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
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++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
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.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
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.
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.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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.
Principles of Object-Oriented Software Development The language C++
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Goals for today’s review
Overview of C++ Polymorphism
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
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.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
 Inheritance  Protected Members  Non-public Inheritance  Virtual Function Implementation  Virtual Destructors  Abstract Base Classes and Interfaces.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & Polymorphism.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
258 3/30/98 CSE 143 Object-Oriented Programming [Chapter 11]
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
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
Class A { public : Int x; A()
Object-Oriented Programming
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
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
C++ Polymorphism Reference and pointer implicit type casting
Lecture 6: Polymorphism
Presentation transcript:

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 types: declared classes, structs, unions (including those provided by C++ libraries) –Native types are “built in” to the C++ language itself: int, long, float, … –A typedef creates new type names for other types 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 –Liskov Substitution Principle: if S is a subtype of T, then wherever you need a T you can use an S

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Polymorphism depends on virtual member functions in C++ –Base class declares a member function virtual –Derived class overrides the base class’s definition of the function Private inheritance creates a form of encapsulation –Class form of the Adapter Pattern –A privately derived class wraps its base class

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath 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 –k is private. It can only be used by methods in class A Class B inherits publicly from A –i is again public to all users of class B –j is again protected. It can be used by methods in class B or its derived classes Class C uses protected inheritance from A –i is now protected in C, so the only users of class C that can access i are the methods of class C –j is again protected. It can be used by methods in class C or its derived classes Class D uses private inheritance from A –i and j are private in D, so users of D cannot access them, only methods of D itself

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Inheritance & Constructor Ordering class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { public: B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} private: int m_j_; }; int main (int, char *[]) { B b(2,3); return 0; }; Class and member construction order –B constructor called on object b in main Passes integer values 2 and 3 –B constructor calls A constructor passes value 2 to A constructor via initializer list –A constructor initializes member m_i with passed value 2 –Body of A constructor runs outputs “A” –B constructor initializes member m_j with passed value 3 –Body of B constructor runs outputs “B”

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Inheritance & Destructor Ordering class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { public: B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} private: int m_j_; }; int main (int, char *[]) { B b(2,3); return 0; }; Class and member destructor order: –B destructor called on object b in main –Body of B destructor runs outputs “~B” –B destructor calls member m_j “destructor” 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 member m_i destructor again a no-op Compare dtor and ctor order –at the level of each class, the order of steps is reversed in ctor vs. dtor –ctor: base class, members, body –dtor: body, members, base class

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Virtual Functions class A { public: A () {cout<<" A";} virtual ~A () {cout<<" ~A";} }; class B : public A { public: B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} }; 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 be overridden in derived class –Overriding only happens when signatures are the same –Otherwise it just overloads the function or operator name Ensures derived class function definition is resolved dynamically –E.g., that destructors farther down the hierarchy get called

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Virtual Functions class A { public: void x() {cout<<"A:x";}; virtual void y() {cout<<"A:y";}; }; class B : public A { public: 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”

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Pure Virtual Functions class A { public: virtual void x() = 0; virtual void y() = 0; }; class B : public A { public: virtual void x(); }; class C : public B { public: 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) Derived classes override pure virtual methods –B overrides x(), C overrides y() Can’t instantiate class with declared or inherited pure virtual functions –A and B are abstract, can create a C Can still have a pointer to an abstract class type –Useful for polymorphism

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Summary: Tips on Polymorphism Push common code and variables up into base classes Use public inheritance for polymorphism Polymorphism depends on dynamic typing –Use a base-class pointer or reference if you want polymorphism –Use virtual member functions for dynamic overriding Use private inheritance only for encapsulation Use abstract base classes to declare interfaces Even though you don’t have to, label each virtual method (and pure virtual method) in derived classes