Learning C/C++ Step-By-Step - Page 16
On this page
16. Step-by-Step C/C++ --- C++ Programming - Polymorphism
Polymorphism
1. Function Overloading 2. Polymorphism 3. Types of polymorphism 4. Normal member functions accessed with pointers 5. Virtual Function 6. Pure Function 7. Assignment and Copy-Initialization 8. The COPY Constructor 9. ‘this’ Pointer |
1. Function Overloading
If a function with its name differed by arguments behavior is called functions polymorphism or function overloading.
// An example program to demonstrate the use of function overloading |
Polymorphism
Polymorphism is one of the crucial features of OOP. It simply means one name, multiple forms. We have already seen how the concept of polymorphism is implemented using overloaded functions and operators. The overloaded member functions are selected for invoking by matching arguments, both type and number. The compiler knows this information 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 functions call at compile time.
Now let us consider a situation where the function name and prototype is the same in both the base and derived classes. For example, considers the following class definitions.
#include <iostream> |
How do we use the member function show( ) to print the values objects of both the classes A and B ? Since the prototype of show( ) is the same in the both places, the function is not overloaded and therefore static binding does not apply. In fact, the compiler does not know what to do and defers the decision.
It would be nice if the appropriate member function could be selected while the program is running. This is known as runtime polymorphism. How could it happen? C++ supports a mechanism known as virtual function to achieve runtime polymorphism. At runtime, when it is known what class objects are under consideration, the appropriate version of the function is called.
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 or dynamic linking because the selection of the appropriate function is done dynamically at runtime.
3. Types of Polymorphism
Polymorphism is of two types namely.
1 | Compile time polymorphism An object is bound to its function call at compile time. |
2 | Runtime polymorphism The selection and appropriate function is done dynamically at run time. |
Dynamic binding is one of the powerful features of C++. This requires the use of pointers to objects. We shall discuss in detail how the object pointers and virtual functions are used to implement dynamic binding.
4. Normal Member Functions Accessed with Pointers
The below program consist of a base class
/* Normal functions accessed from pointer */ |
The above program demonstrates:
• A BASE class • DERIVED1, DERIVED2 classes derived from BASE • Derived classes objects (d1,d2) • BASE class pointer *b |
|
Output You are in BASE class |
5. Virtual Function
Virtual means existing in effect but not in reality.
A member function can be made as virtual function by preceding the member function with the keyword virtual.
/* Polymorphism with Classes (Virtual polymorphism) */ |
|
Output class D method Show() |
6. Pure Function
A function defined in a base class and has no definition relative to derived class is called pure function. In simple words a pure function is a virtual function with no body.
#include <iostream> |
Bptr -> show() is the default executable function from Base
Bptr -> disp() is the default executable function from Base but it is declared as a virtual pure function so at runtime Derived class's disp() will be called.
/* Program to demonstrate the advantage of pure virtual functions */ |
7. Assignment and Copy-Initialization
The C++ compiler is always busy on your behalf, doing things you can’t be bothered to do. If you take charge, it will defer to your judgement; otherwise it will do things its own way. Two important examples of this process are the assignment operator and the copy constructor.
You’ve used the assignment operator many times, probably without thinking too much about it. Suppose a1 and a2 are objects. Unless you tell the compiler otherwise, the statement.
a2 = a1; // set a2 to the value of a1
Will cause the compiler to copy the data from a1, member-by-member, into a2. This is the default action of the assignment operator, =.
You’re also familiar with initializing variables, initializing an object with another object, as in
alpha a2(a1); // initialize a2 to the value of a1
Causes a similar action. The compiler creates a new object, a2, and copies the data from a1, member-by-member, into a2. This is the default action of the copy constructor.
Both these default activities are provided, free of charge, by the compiler. If member-by-member copying is what you want, you need take no further action. However, if you want assignment of initialization to do something more complex, then you can override the default functions. We’ll discuss the techniques for overloading the assignment operator and the copy constructor separately.
Overloading the Assignment Operator
// Overloading the Assignment ( = ) Operator |
|
Output: a2 = 37 |
8. The COPY Constructor
As we discussed, we can define and at the same time initialize an object to the value of another object with two kinds of statement:
alpha a3(a2); // Copy initializing
alpha a3 = a2; // copy initialization, alternate syntax
Both styles of definition invoke a copy constructor: that is, a constructor that copies its argument into a new object. The default copy constructor, Which is provided automatically by the compiler for every object, performs a member-by-member copy. This is similar to what the assignment operator does; the difference is that the copy constructor also creates also creates a new object.
The following example demonstrates the copy constructor.
#include <iostream> |
The above program overloads both the assignment operator and the copy constructor.
The overloaded assignment operator is similar to that in the past example.
9. ‘this’ Pointer
C++ uses a unique keyword called this to represent an object that invokes a member functions. This is a pointer that points to the object for which this function was called.
This pointers simply performs make task to return object it self.
The following program defines i, j objects and i is assigned with the value of 5 and the entire object of i is assigned by its member function to j
#include <iostream> |
Ref: Object-oriented Programming in Turbo C++: Robert Lafore