






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Polymorphism
Object Oriented Programming
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
Object Oriented Programming
Normal Member Functions Accessed with Pointers
Normal Member Functions Accessed with Pointers
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
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.
Polymorphism
Object Oriented Programming
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
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
int
main(){
Square
S(4);
Cube
C(8);
S.Area();C.Area();
}
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.