0% found this document useful (0 votes)
9 views5 pages

Chapter 9

The document discusses pointers, virtual functions, and polymorphism in C++. It explains the concepts of early binding (compile-time polymorphism) and late binding (run-time polymorphism) through virtual functions, as well as the rules governing virtual functions and their usage. Additionally, it highlights the importance of using base class pointers to access derived class functions for achieving run-time polymorphism.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Chapter 9

The document discusses pointers, virtual functions, and polymorphism in C++. It explains the concepts of early binding (compile-time polymorphism) and late binding (run-time polymorphism) through virtual functions, as well as the rules governing virtual functions and their usage. Additionally, it highlights the importance of using base class pointers to access derived class functions for achieving run-time polymorphism.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

9.

Pointers, Virtual
Functions and
Polymorphism
The overloaded member functions are ‘selected’ for invoking
by matching arguments, both type and number. This
information is known to the compiler at the compile time and,
therefore, compiler is able to select the appropriate function
for a particular call at the compile time itself. This is called
early binding or static binding or static linking. Also known as
compile time polymorphism, early binding simply means that
an object is bound to its function call at compile time.

It would be nice if the appropriate member function could be


selected while the program is running. This is known as run
time polymorphism.
C++ supports a mechanism known as virtual function to
achieve run time polymorphism.

At run time, when it is known what class objects are under


consideration, the appropriate version of the function is
invoked. Since the function is linked with a particular class
much later after the compilation, this process is termed as
late binding. It is also known as dynamic binding because the
selection of the appropriate function is done dynamically at
run time.

We can locate asterisk (*) immediately before the pointer


variable, or between the data type and the pointer variable,
or immediately after the data type. It does not cause any
effect in the execution process.
The pointers, which are not initialized in a program, are called
Null pointers. Pointers of any data type can be assigned with
one value i.e., ‘0’ called null address.

There is no error checking of array bounds in C++. Suppose


we declare an array of size 25. The compiler issues no
warnings if we attempt to access 26th location. It is the
programmer’s task to check the array limits.

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. Here, the function is
passed as a pointer. The function pointers cannot be
dereferenced. C++ also allows us to compare two function
pointers.

Like other variables, we can declare a function pointer in C+


+. It takes the following form:
data_type (*function_name)( );

Example:
int (*num_function(int x));

Since *ptr is an alias of x, we can also use the following


method:
(*ptr).show( );

C++ uses a unique keyword called this to represent an


object that invokes a member function. this is a pointer that
points to the object for which this function was called. For
example, the function call A.max( ) will set the pointer this
to the address of the object A.
This unique pointer is automatically passed to a member
function when it is called. The pointer this acts as an implicit
argument to all the member functions.

Pointers to objects of a base class are type-compatible with


pointers to objects of a derived class.

If B is a base class and D is a derived class from B, then a


pointer declared as a pointer to B can also be a pointer to D.

Although a base pointer can be made to point to any number


of derived objects, it cannot directly access the members
defined by a derived class.

When we use the same function name in both the base and
derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal
declaration. When a function is made virtual, C++
determines which function to use at run time based on the
type of object pointed to by the base pointer, rather than the
type of the pointer. Thus, by making the base pointer to point
to different objects, we can execute different versions of the
virtual function.

One important point to remember is that, we must access


virtual functions through the use of a pointer declared as a
pointer to the base class. Why can’t we use the object name
(with the dot operator) the same way as any other member
function to call the virtual functions? We can, but remember,
run time polymorphism is achieved only when a virtual
function is accessed through a pointer to the base class.

Rules for Virtual Functions


When virtual functions are created for implementing late
binding, we should observe some basic rules that satisfy the
compiler requirements:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even
though it may not be used.
6. The prototypes of the base class version of a virtual
function and all the derived class versions must be identical.
If two functions with the same name have different
prototypes, C++ considers them as overloaded functions,
and the virtual function mechanism is ignored.
7. We cannot have virtual constructors, but we can have
virtual destructors.
8. While a base pointer can point to any type of the derived
object, the reverse is not true. That is to say, we cannot use a
pointer to a derived class to access an object of the base
type.
9. When a base pointer points to a derived class,
incrementing or decrementing it will not make it to point to
the next object of the derived class. It is incremented or
decremented only relative to its base type. Therefore, we
should not use this method to move the pointer to the next
object.
10. If a virtual function is defined in the base class, it need
not be necessarily redefined in the derived class. In such
cases, calls will invoke the base function.

It is normal practice to declare a function virtual inside the


base class and redefine it in the derived classes. The function
inside the base class is seldom used for performing any task.
It only serves as a placeholder. For example, we have not
defined any object of class media and therefore the function
display( ) in the base class has been defined ‘empty’. Such
functions are called “do-nothing” functions.
A “do-nothing” function may be defined as follows:
virtual void display( ) = 0;

Such functions are called pure virtual functions. Remember


that a class containing pure virtual functions cannot be used
to declare any objects of its own. As stated earlier, such
classes are called abstract base classes.The main objective of
an abstract base class is to provide some traits to the derived
classes and to create a base pointer required for achieving
run time polymorphism.

A constructor cannot be virtual.

Run time polymorphism is achieved only when a virtual


function is accessed through a pointer to the base class. It
cannot be achieved using object name along with the dot
operator to access virtual function.

You might also like