Docsity

Polymorphism-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Toggle sidebar
bg1
5/13/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 14
Polymorphism
Introduction
The ability to: 1) create a function which has
more than one form 2)
use the same syntax
more than one form
,
2)
use the same syntax
to do different things (depending upon the
context) is called Polymorphism
It is closely related to Inheritance, such that a
base class’s pointer is used to invoke routines in
its derived classes
Usman Younis
its derived classes
Functions/routines implemented in a specific
derived class are called – late-binding
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Polymorphism-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 14

Polymorphism

Introduction

„ The ability to: 1) create a function which has

more than one formmore than one form, 2) 2) use the same syntaxuse the same syntax

to do different things (depending upon the

context) is called Polymorphism

„ It is closely related to Inheritance, such that a

base class’s pointer is used to invoke routines in

its derived classes

Usman Younis

its derived classes

„ Functions/routines implemented in a specific

derived class are called – late-binding

Inheritance – to – Polymorphism

„ A base class’s pointer can access any of its

child’s objectchild s object

„ When a base class pointer is used to store one

of it’s children, any method declared in the

base class can be called

„ However, any methods that are not declared in

the base class can not be called doesn’t help

Usman Younis

the base class can not be called – doesn t help

us towards polymorphism. Solution? Coming

next

Using Base class’s pointer

class Base { public: Base()

class Derv1:public Base { public: Base() Derv1() { cout<<"this is base \n"; }

void a_function() { cout<<"I am base class function \n"; }

~Base()

Derv1() { cout<<"this is Derv1 \n"; }

void a_function() { cout<<"I am a Derv1 class function \n"; }

void a2_func()

Usman Younis

{ cout<<"Base is now destructed \n"; } };

{ cout<<"Another function \n"; }

~Derv1() { cout<<"Derv1 is now destructed \n"; } };

Base Class’s Pointer (Contd..)

Extending the previous

code

class Derv2: public Derv

void main() { Base* ptr; { public: Derv2() { cout<<"this is Derv2 \n"; }

void a_function() { cout<<"I am a Derv2 class function \n"; }

Derv2 d;

ptr = &d;

ptr->a_function();

t D 1

Usman Younis

function \n ; }

~Derv2() { cout<<"Derv2 is now destructed \n";} };

ptr = new Derv1;

ptr->a_function();

}

Base Class’s Pointer (Contd..)

„ In the previous example, the pointer is statically bound to the Base classstatically bound to the Base class, even even when the newly created derived is pointed by it „ This is called compile-time or static-binding, i.e, the variables are bound to a specific type when the code is compiled

Usman Younis

yp p

„ This binding can be changed at the run- time, called dynamic- or late-binding. Working towards virtual functions

Virtual Functions

„ A virtual function is a function that is declared with a keyword virtual in the base class and then re-a keyword virtual in the base class and then re defined by the derived class „ When a virtual function is invoked by a base class pointer, the type of an object being pointed at determines which version of the function is called „ The keyword virtual is only required in the base

Usman Younis

class, but many programmers use it in derived classes, simply to remind themselves that the function is virtual

Virtual Functions (contd..)

„ Used to support polymorphism with pointers and referencesand references

„ Ensures derived class function definition is resolved dynamically

„ When you declare a function as a virtual in the base class, you indicate to the compiler

Usman Younis

that you want dynamic binding for that function in any class that is derived from this base class

Virtual Functions

„ Not every function can be declared virtual

„ Following functions cannot be virtualll f b l

‰ Non-member functions ‰ Static member functions ‰ Constructors, and ‰ Friend functions

Usman Younis

Virtual Destructors

„ While using

polymorphism,

class A { public:

p y p , A () {cout<<" A";}

destructors also need to

be declared as virtual in

the base class

„ This will ensure that the

appropriate destructor is

virtual ~A () {cout<<" ~A";} }; class B : public A { public: B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} };

Usman Younis

appropriate destructor is

called and the object is

properly destroyed

}; void main () { A *ap = new B; //prints “A B” delete ap; //prints “~B ~A” //would have only printed //"~A“ if non-virtual }

Pure Virtual Functions

„ A pure virtual function is a function that has no

definition and requires that child classes mustdefinition and requires that child classes must

override it

„ In the declaration of a class, a pure virtual

function is declared as following:

‰ virtual void print() = 0;

In this case you are not allowed to write a

Usman Younis

„ In this case, you are not allowed to write a

definition for print() in the base class; however,

it must be written for every child of this class

Pure Virtual Functions

„ Pure virtual functions are used to give an abstract view of the class hierarchyabstract view of the class hierarchy

„ In this case base is only used to access the derived classes, and it (base) is never instantiated

„ Example:

Usman Younis

‰ Please see the handout “Polymorphism - 2”

Abstract Class Constructor

„ If objects cannot be instantiated of an abstract class

then why constructor?then why constructor?

„ The constructor for abstract class is there to initialize

its data members

„ To make this work, base class’s constructor will be

called through initializer list of a derived class

constructor

If you try to call the constructor for an abstract class

Usman Younis

„ If you try to call the constructor for an abstract class

from anywhere else you get an error message from the

compiler

„ In the example handout “Polymorphism - 2”, Resistor is

an Abstract Class

Summary

„ Push common code and variables up into the

base classes

„ Polymorphism depends upon dynamic binding

„ Use a base-class pointer or reference if you

want polymorphism

„ Use virtual member functions for dynamic

overriding

Usman Younis

overriding

„ 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