Chapter 4. Polymorphism
Chapter 4. Polymorphism
POLYMORPHISM
Er. Ganga Gautam
Scope resolution ::
operator
Size of operator sizeof
Dot operator .
Pointer selector *
Ternary operator ?:
• Where;
– returnType is the type of the data that is to be returned
by the specified operator() function.
– Operator is the function name for operator overloading.
– symbol is the operator that is being overloaded (i.e. + , ++,
etc)
– arguments list receives the values.
Return Type:
• The return type of
operator function is
always a class type,
• because the operator
overloading is only for
objects.
• This was because the C++ compiler ignores the content of b (which is address
of obj1 after statement p=&obj1; ) and execute the function which is inherited
from class Base because the type b matches with class Base.
• Similarly, after statement p=&obj2; and p->show(); , the compiler again
executes the function which is inherited from class Base.
• Whenever this type of situation occurs (i.e.in derived class, there are two
functions, both have the same name: one inherited from base and another of
its own, and the pointer is Base type), then we want to execute the function
through pointer, compiler chooses the function of Base class. So, to overcome
this situation, we have to make the base class function as virtual.
• Thus the use of same function call at different places produces different result
at run time. Hence, it provides the concept of polymorphism.
main( )
• Here ob is a polymorphic
variable.