Polymorphism
• Polymorphism: one name but different forms.• Polymorphism is implemented by function over loading and operator overloading – discussed earlier
• Giving same name to functions with different no or types of arguments
• This information is known to compiler at compile time and compiler is able to select the appropriate function at function call.
• It is called compile time polymorphism-Early binding or Static Binding.
Run Time Polymorphism
• If appropriate member function are chosen at run time rather than compile time, this is known as Runtime polymorphism or Late Binding or Dynamic Bindings• For this mechanism we chose member function as virtual.
• In C++, polymorphism also indicates the form of a member function that can be changed at runtime. Such member functions are called virtual functions and the corresponding class is called polymorphic class.
• The objects of the polymorphic class, address by pointers, change at runtime and respond differently for the same message. Such mechanism requires postponement of binding of a function call to the member function declared as virtual until runtime.
Polymorphism(view in picture)
Virtual Function
• A virtual function is a function that appears to exists but does not exist in reality.• Declaration of virtual function is done in base class as.
class Test
{
public:
……………
virtual retrun_type function_name(args……..)
{
//function body
}
};
• When we use same function in both base class and derived class and access them using pointer of base class objects assigning the address of objects of derived class, we expect that the function should perform the operation of which base pointer is assigned the address but we can not get such result using general member function. For this we can use virtual functions.
• Virtual function is declared in base class with keyword virtual and without keyword virtual in other derived class.
• An object pointer of base class is used for assigning the address of the object of derived class and to invoke the member function of same name for different task.
Virtual Function: Example
Virtual Function: Example
Abstract classes and pure virtual function
• When the objects of base class are never instantiated, such a class is called abstract base class or simply or simply abstract class.• An abstract class only exists to act as a parent of derived classes from which objects are instantiated.
• It may also provide interface for class hierarchy.
• To make a class abstract so that object instantiation is not allowed and derivation of child classes are allowed, at least one pure virtual function should be placed in the class.
Pure virtual function
• A pure virtual function is one with the function expression = 0 is added to the declaration of virtual function.• The syntax of declaration of pure virtual function and making a class abstract is:
class A
{
public:
virtual void show() = 0 ;
// above is a pure virtual function
};
• With the presence of pure virtual function show(), class A above becomes abstract class.
• In the previous declaration , class A become abstract since there is presence of pure virtual function show ( ).
• The expression = 0 has no any other meaning the equal sign = 0 does not assign 0 to function show (). It is only method to tell the compiler that show () is pure virtual function hence class A is abstract class.
• All classes with pure virtual function are also known as concrete classes.
• A pure virtual function has following properties.
1. A pure virtual function has no implementation in the base class.
2. It acts as an empty bucket (virtual function is partially filled bucket) that the derived class are supposed to fill it.
3. A pure virtual function can be invoked by its derived class.
Pure Virtual Function: Example
Virtual destructors
• Since destructors are member functions, they can be made virtual with placing keyword virtual before it. The syntax isvirtual ~ classname ( ) ; // virtual destructor.
• The destructor in base class should always be virtual.
• If we use delete with a base class object to destroy the derived class object, then it calls the delete calls the member function destructor for base class.
• This causes the base class object to be destroyed.
• Hence making destructor of base class virtual, we can prevent such miss-operation.
Virtual Destructor:Example
In the above example,• pBase stores address of object of Derv1 class.
• Delete pBase destroy the Base object i.e. calls the destructor of base class.
• If the destructor is made virtual by the line virtual ~Base ( ) ;
then,
delete pBase ;
Simply calls the destructor of Derv class first and the output is now:
Derv destroyed.
Base destroyed.
Virtual Base class
• In multiple inheritance, if a base class parent derives its two child class then another class is derived from two child (e.g. A is parent for child B and C , then D is derived from B and C as parent) , it defines multipath inheritance.• When member function of class D want to access data member of parent class A, then problem arises due to ambiguity.
• To resolve such ambiguity we use virtual base class.
• A virtual base class is one from which classes are derived virtually and ambiguity can be resolved.
Virtual Base class:Syntax
class A.
{
// body of class A
};
class B: virtual public A
{
// Body of B
};
class C:virtual public A
{
// Body of class C
};
class D: public B; public C.
{
};
Example: Virtual Base
Explanation
• In above example, when the member function of grandchild attempts to access base data in parent, each child1 and child2 inherits the copy of basedata.• Since grandchild class is derived from both child1 and child2, so attempting to access base data becomes ambiguous in grandchild.
• This ambiguity can be resolved by making virtual base class as:
class child1: virtual public parent
{ };
class child2: virtual public parent
{ };
• The use of virtual in these two class causes them to share a single common copy of base data.
• So attempt to access base data in grandchild is not ambiguous.
'this' pointer
• The member functions of every object have access to a magic pointer named 'this' which points to the object itself.• The this pointer is implicitly defined in each member function.
• Every member function of a class is born with a pointer called this which points to the object with which the member function is associated.
• When member function is invoked, it comes into existence with the value of this set to the address of the object for which it is called.
• Method of accessing member of class with 'this' is as in figure below.
Use of “this Pointer”
Example: this Pointer
Using this for returning values:
• For returning values from member functions, this can play important role.• When an object is local to a function, the object will be destroyed when function terminates. So it is necessary for a more permanent object while returning it by reference.
• Consider a function add() for addition of two complex object called as c3=c1. add(c2); Where c1, c2, c3 are complex no. objects.
• Our function will be as.
complex complex:: add(complex c2)
{
real= real+ c2.real;
imag=imag +c2.imag,
return complex(real, imag);
}
• It adds c2 to a default object and returns the updated default object by creating nameless temporary object by statement
return complex(real, imag);
• It can be replaced by the statement simply by
return *this;
// returns object by value
• Now definition of add will be
complex add(complex c2)
{
real=real+c2.real;
imag=imag +c2.imag,
return*this;
}
0 Comments