CH - 9-Runtime Polymorphism
CH - 9-Runtime Polymorphism
Virtual Functions
Introduction
• Polymorphism : It is one of the crucial features of
OOP. It simply means ‘one name, multiple forms’.
• The overloaded member functions are selected for
invoking by matching the arguments, both type
and number.
• The information is known to the compiler at
compile time.
• This is called early binding or static binding or
static linking and polymorphism.
• When two functions with the same name are used in
two different classes, they can be defined using class
resolution operator.
• Here the function is not overloaded and so static
binding does not apply.
• If we want a member function could be selected while
the program is running , then we have to use virtual
function.
• This is known as run time polymorphism.
• // Virtual Functions and // Run-time Polymorphism
• #include <iostream.h>
▫ // base class
class base
{ public: int a; };
▫ // derived class
class derived:public base
{ public: int b; };
▫ // main
void main()
{ base b; derived d;
▫ // base class pointer
base *bptr;
// pointer pointing to base's object
bptr=&b;
bptr->a=10;
// pointer pointing to derived's object
bptr=&d; // still is able to access the members of the base class
bptr->a=100; }
• // Using Virtual functions to achieve run-time Polymorphism
• #include <iostream.h>
// base class
class base
{
public:
virtual void func()
{ cout<<"Base's func()\n"; }
};
// derived class
class derived:public base
{
public:
void func()
{
cout<<"Derived's func()\n"; }
};
• // main
void main()
{
int ch=0;
base b;
derived d;
// base class pointer
base *bptr;
while(ch!=3)
{
cout<<"1> Call Base's func\n";
cout<<"2> Call Derived's func\n";
cout<<"3> Quit\n"; cin>>ch;
switch(ch)
{
case 1: // point to base's object
bptr=&b; break;
case 2: // point tp derived's object
bptr=&d; break;
default: bptr=&b;
} // call whichever function // user has chosen to call
bptr->func(); } }
Pointers
• To achieve the dynamic binding, it is required to use the
pointer to objects and virtual functions.
• Pointer is a derived data type that refers to another data
variable by storing the variable’s memory address rather
than the data.
• Like C, a pointer variable can also be used to refer another
pointer in c++.
• Pointers provide an alternative approach to access other
data objects.
Pointer to Object
• Normal Objects cannot help in achieving runtime
polymorphism. It is possible to achieve it only by setting
or defining pointer to objects,
• Pointer to object is a variable containing an address of an
object.
• It is similar to a pointer to any other variable. We can use
normal address-of operator to get the address of an
object.
• We can define a pointer to an object and can assign it the
address of an object.
Declaring Pointers
• The declaration of a pointer is based on the data type of
the variables it points to.
• Syntax : - datat-type *pointerVariable;
• A pointer is able to point to only one data type at a specific
time.
• Eg :- int *ptr; // declare
ptr = &a; // initialize
int *nptr;
nptr = number[0];
Nptr points to the 1st element of an array.
Array of pointers
• An array of pointers represent the collection of
addresses.
• An array of pointers point to an array of data
items.
• Each element of the pointer array points to an
item of the data array.
• Data items can be accessed either directly or by
dereferencing the elements of pointer array
Pointers to Functions
• The pointer to function is known as callback function. We
can use these function pointers to refer to a function.
• Using function pointers, we can allow a c++ program to
select a function dynamically at run time.
• We can also pass a function as an argument to another
function (as pointer).
• There are two types of function pointers, function that
points to static member functions and function pointers
that point to non-static member functions
• For non-static member function requires hidden
argument.