Polymorphism, Schemes and Mind Maps of Object Oriented Programming

In C++, polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the ...

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

snehaaaa
snehaaaa 🇺🇸

4.7

(19)

239 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
297
Polymorphism
Polymorphism
8
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Polymorphism and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity!

Polymorphism

Polymorphism

Polymorphism

Object Oriented Programming

Content

Content

Polymorphism

Virtual Members

Abstract Class

300

Polymorphism

Object Oriented Programming

Polymorphism means “taking many shapes”. The minister’s singleinstruction is polymorphic because it looks different to differentkinds of personnel.

Typically,

polymorphism

occurs

in

classes

that

are

related

by

inheritance. In C++, polymorphism means that a call to a memberfunction will cause a different function to be executed depending onthe type of object that invokes the function.

This sounds a little like function overloading, but polymorphism is adifferent, and much more powerful, mechanism. One differencebetween

overloading

and

polymorphism

has

to

do

with

which

function to execute when the choice is made.

With function overloading, the choice is made by the compiler(compile-time). With polymorphism, it’s made while the program isrunning (run-time).

Polymorphism

Polymorphism

Polymorphism

Object Oriented Programming

Normal Member Functions Accessed with Pointers

Normal Member Functions Accessed with Pointers

class

Square

Square

// Base Class

protected:

double edge;

public:

Square(double e):edge(e){ } //Base class constructordouble

a

area

rea

(){ return( edge * edge ) ; }

}; class

Cube

Cube

: public

Square

Square

{ // Derived Class

public:

Cube(double e):Square(e){} // Derived class cons.double

a

area

rea

(){ return( 6.0 * edge * edge ) ; }

Polymorphism

Object Oriented Programming

Let’s make a single change in the program: Place the keyword

virtual

in front of the declaration of the area() function in the base class.

class

Square Square

// Temel sinif

protected:

double edge;

public:

Square(double e):edge(e){ } // temel sinif kurucusu

virtual virtual

double

area area

(){ return( edge * edge ) ; }

}; class

Cube Cube

: public

Square Square

{ // Turetilmis sinif

public:

Cube(double e):Square(e){} // Turetilmis sinif kurucusudouble

area area

(){ return( 6.0 * edge * edge ) ; }

Virtual Member Functions Accessed with Pointers

Virtual Member Functions Accessed with Pointers

Polymorphism

Object Oriented Programming

int main(){

Square S(2.0)

Cube

C(8.0)

Square *ptr

char

c

cout << “Square or Cube"; cin >> c

if (c==‘s') ptr=&S

else

ptr=&C

ptr

Area();

square.cpp

Polymorphism

Object Oriented Programming

Now, different functions are executed, depending on the contents ofptr. Functions are called based on the contents of the pointer ptr, noton the type of the pointer. This is polymorphism at work. I’ve madeprint() polymorphic by designating it virtual.

How does the compiler know what function to compile? In e81.cpp,the compiler has no problem with the expression

ptr->print();

It always compiles a call to the print() function in the base class. Butin e82.cpp, the compiler doesn’t know what class the contents of ptrmay be a pointer to. It could be the address of an object of theTeacher class or the Principal class. Which version of print() doesthe compiler call? In fact, at the time it’s compiling the program, thecompiler doesn’t know what to do, so it arranges for the decision tobe deferred until the program is running.

Late Binding

Late Binding

Polymorphism

Object Oriented Programming

Late Binding

Late Binding

At runtime, when the function call is executed, code that thecompiler placed in the program finds out the type of the objectwhose address is in ptr and calls the appropriate print() function:Teacher::print() or Principal::print(), depending on the class of theobject.

Selecting a function at runtime is called

late binding

or

dynamic

binding

. (Binding means connecting the function call to the

function.)

Connecting to functions in the normal way, during compilation, iscalled

early binding

or

static binding

. Late binding requires a small

amount of overhead (the call to the function might take somethinglike 10 percent longer) but provides an enormous increase in powerand flexibility.

Polymorphism

Object Oriented Programming

How It Works

How It Works

With virtual functions, things are more complicated. When a derivedclass with virtual functions is specified, the compiler creates a table—an array—of function addresses called the

virtual table

The Teacher and Principal classes each have their own virtual table.There is an entry in each virtual table for every virtual function in theclass. Objects of classes with virtual functions contain a pointer to thevirtual table of the class. These object are slightly larger than normalobjects.

In the example, when a virtual function is called for an object ofTeacher or Principal, the compiler, instead of specifying whatfunction will be called, creates code that will first look at the object’svirtual table and then uses this to access the appropriate memberfunction address. Thus, for virtual functions, the object itselfdetermines what function is called, rather than the compiler.

Polymorphism

Object Oriented Programming

class Principal : public Teacher{

//

Derived class

string *SchoolName;

public:

void read();

//

Virtual function

void print() const;

//

Virtual function

};

Example:

Assume that the classes Teacher and Principal contain two virtual functions.

class Teacher{

//

Base class

string *name;int numOfStudents;

public:

virtual

void read();

//

Virtual function

virtual

void print() const;

//

Virtual function

};

&Teacher::read&Teacher::print

Virtual Table of

Teacher

&Principal::read&Principal::print

Virtual Table of

Principal

Polymorphism

Object Oriented Programming

Be aware that the virtual function mechanism works onlywith pointers to objects and, with references, not with objectsthemselves.

int

main(){

Square

S(4);

Cube

C(8);

S.Area();C.Area();

}

Don’t Try This with Objects

Don’t Try This with Objects

Calling virtual functions is a time-consuming process, becauseof indirect call via tables. Don’t declare functions as virtual ifit is not necessary.

Polymorphism

Object Oriented Programming

class

Square Square

// Base

protected:

double edge;

public:

Square(double e):edge(e){ } // Base Class Constructor

virtual virtual

double

Area Area

(){ return( edge * edge ) ; }

}; class

Cube Cube

: public

Square Square

{ // Derived Class

public:

Cube(double e):Square(e){} // Derived Class Constructordouble

Area Area

(){ return( 6.0 *

Square::Area Square::Area()

Warning

Here,

Square::Area Square::Area()

is not virtual

Polymorphism

Object Oriented Programming

int main(){

Circle

c1(50);

Square s1(40);Circle

c2(23);

Square s2(78);Square *listPtr;

// Pointer of the linked list

/*** Construction of the list ***/listPtr=&c1;c1.next=&s1;s1.next=&c2;c2.next=&s2;s2.next=0L;while (listPtr){ // Printing all elements of the list

cout << listPtr->Area() << endl ;listPtr=listPtr->next;

example27.cpp

316

Polymorphism

Object Oriented Programming

To write polymorphic functions wee need to have derived classes.

But sometimes we don’t need to create any base class objects, butonly derived class objects. The base class exists only as a startingpoint for deriving other classes. ►

This kind of base classes we can call are called an

abstract class

which means that no actual objects will be created from it. ►

Abstract classes arise in many situations. A factory can make a

sports car or a truck or an ambulance, but it can’t make a genericvehicle. The factory must know the details about what

kind

of vehicle

to

make

before

it

can

actually

make

one.

Similarly,

you’ll

see

sparrows, wrens, and robins flying around, but you won’t see anygeneric birds. ►

Actually, a class is an abstract class only in the eyes of humans.

Abstract Classes

Abstract Classes