C++ polymorphism

C++ Polymorphism

Polymorphism is one of the crucial features of Object oriented programming, it simply means one name multiple forms. we have already seen in previous how the concept of polymorphism is implemented using the overloaded functions and operators. The overloaded member functions are selected for invoking by matching argument, both type and number.
This information is known to the compiler at the compile time and therefore,compiler is able to selected the appropriate functions for a particular call at the compile time itself. This is know(called) early binding or static linking or static binding . it is also known as compiler time polymorphism, early binding means that an object is bound to its function call at compile time

now let us take a situation where the function name and prototype is the same in both the base class and derived class. for example consider the following class definition.

Class A
{
int X;
public:
voidshow()
{
…….
……. // show() method in the base class
};
Class B: class A
{
int y;
public:
void show()
{
…….
……. // show in derived class
};

How do we use the member function show() to print the value of objects of both the class A and B? Since the prototype of show() is the same in both the classes, the function is not overloaded and therefore static binding does not apply here.
we have seen earlier that in such type situations, we may use the class resolution operator to specify the class whenever invoking the functions with the derived class objects

It would be good if the appropriate member function could be selected while the program is running.
This is know as runtime polymorphism. How could it happen? C++ language supports a mechanism know as virtual function to achieve run time polymorphism, please refer figure

polymorphism picture

At the run Time when it’s known what class objects are under consideration, The appropriate version of the function is call. since the function is linked with a particular class later after the compilation this process is called as late binding. It is also called as dynamic binding because the selection of the appropriate function is done dynamically at run time

dynamic binding is one of the most powerful feature of c++ language. This requires the use of pointers to objects. We shall discuss in detail how the object pointers and virtual function are used to implement dynamic binding.

POINTERS TO OBJECTS

We have already seen previous how to use pointers to access the class members. As stated earlier ,a pointer can point to an object created by a class. Consider the following statement:

item x;

where item is a class and x is an object defined to be of type item. Similarly we can definer pointer it_ptr of type item as follows:

item *it_ptr;

Object pointers are very useful in creating objects at run time. We also use an object pointer to access public members of an object. Consider an example of a class item defined as follows:

class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code = a;
price = b;
}
void show(void)
{
cout « "Code : " « code « << "Price: " « price « "\n\n";
}
};

let us declare an item variable x and a pointer ptr to x as follows

item *ptr=&x;

The pointer ptr is initialized with the address of x

we can refer to the member functions of item in two ways, one by using the dot operator and the object, and another by using the arrow operator and the object pointer.

The statements

x.getdata(100,75.50);
x.show();
are equivalent to
ptr->getdata(100,75.50);
ptr->show();
since *ptr is an alias name of x, for example we can also use the following method
(*ptr).show()

The parentheses are need because the dot operator has higher precedence than the indirection operator (*.)

We can also create the object using pointers and new operator as follows

item *ptr=new item;
(*ptr).show()

This statement allocates enough memory for the data member in the object structure and assigns the address of the memory space to ptr. Then ptr can be used to refer to the members as shown below

ptr->show();

if a class has a constructor with arguments and does not include an empty constructor, then we must assign the address of the memory space to ptr. Then we must supply the arguments when the object is created

we can also create an array of objects using using pointers. for example the statement

item *ptr=new item[10]; // array of 10 objects

creates memory space for an array of 10 objects of item. Remember in such cases of the class contains constructors, it must also contain an empty constructor

POINTERS TO OBJECTS
#include< iostream>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code=a;
price=b;
}
void show()
{
cout << "Code:" << code << "\n";
cout << "price:" << price << "\n";
}
};
const int size=2;
int main()
{
item *p=new item[size];
item *d=p;
int x,i;
float y;
for(i=0;i < size; i++) {
cout << "input code and price for item" << i+1;
cin >> x >> y;
p->getdata(x,y);
p++;
}
for(i=0;i < size;i++)
{
cout << "Item:" << i+1<<"\n";
d->show();
d++;
}
}

the output :
input code and price for item1 40 500
input code and price for item2 50 600

Item:1
code:40
price:500
item:2
code:50
price:600

In the above program we create dynamically for two objects of equal size. but this may not be the case always for example the objects of a class that contain character strings would not be of the same size. In such cases, we can define an array of pointers to objects that can be used to access the individual objects

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise