C++ TokensOperatorsExpressionsOperator overloadingControl statementsC++ functionsClass and objectsConstructor and destructorsOperator OverloadingType conversionsInheritanceAbstractionPolymorphismpointersInput/outputFile HandlingTemplatesException handling |
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. 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.
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.
It would be good if the appropriate member function could be selected while the program is running. 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:
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:
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:
let us declare an item variable x and a pointer ptr to x as follows
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
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
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
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
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
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 |