Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Operator, Overloading, Member, Functions, Argument, Friend, Private, Complex, Compiler

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 10

Toggle sidebar

Related documents


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

void main()

{{

Base* ptr;

Derv1 d;

ptr = &d;

//a pointer variable

//this is base

//this is Derv

Usman Younis

ptr &d;

ptr->a_function();

}

//I am base class function

//Base is now destructed

//Derv1 is now destructed

Base Class’s Pointer (Contd..)

„ Base class’s pointer can access any of its derived classderived class, doesn’t matter where it is doesn’t matter where it is located down the hierarchy

„ However, it is not possible other way round

„ The inheritance should be public (access specifier)

Usman Younis

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 and Polymorphism

Example

class Access { public:

class subtraction: public Access { private: public: int x; virtual int result(int a) { return 0; } };

class addition: public Access { private:

int x; public: subtraction() : x(20) { } int result(int a) { return x - a; } }; class division: public Access { private:

Usman Younis

private: int x; public: addition() : x(20) { } int result(int a) { return x + a; } };

private: int x; public: division() : x(20) { } int result(int a) { if(a != 0) return x / a; else return 0; } };

Virt Funcs & Polymorphism (Contd..)

void main() { Access* access;

addition add; subtraction sub; division div;

access = &add;

cout<<access->result(10)<<endl; 30

Usman Younis

access = ⊂ cout<<access->result(10)<<endl;

access = ÷ cout<<access->result(20)<<endl; }

10

1

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 Classes

„ As understood by its name, an Abstract Class is used to

provide an abstract view/access to the laterprovide an abstract view/access to the later

inheritance

class an_Abstract { //some data and pure virtual functions, which must be defined //in each child class };

Usman Younis

class Child: public an_Abstract { //some data and definitions of pure virtual functions };

an_Abstract* ptr = new Child();

Abstract Classes (contd..)

„ Objects of an Abstract Class cannot be

instantiatedinstantiated

„ Since we can’t create objects of an abstract

class, we can’t use it as function parameters or

a return type

„ However, pointers or references to an abstract

class can be used as parameters or a return

Usman Younis

class can be used as parameters or a return

type

„ An abstract class has no use until it is extended

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