c++ :: polymorphism
/** \brief
*.Polymorphism
*.- given identical instructions, should take different actions
*.- call to a member function will cause a different function.
*. to be executed depending on the type of object that invokes the function
*.- implemented using _virtual_
*. if i don't give virtual, polynmorphism doesnot happen
*. and always the code of base class will be executed.
*. make the function virtual and declare it in the body of derived class
*.- descision of which functuion to call is made at run time.
*. called late/dynamic binding. overhead (upto 10% slower)
*.- early/static binding - the normal way
*.- only work with pointers [see in main.cpp]
*
*.Only a virtual function can be made abstract (=0)
*.Only functions can be virtual, class/variables cannot be
*.`virtual' can only be specified for functions
*
*.Virtual function of base class can be called using scope resolution
*.eg Base::method();
*
*.Constructor cannot be virtual.
*.Destructors should be virtual. _imp_
*.- it handles the deletion of the derived class and base class (in order)
*
* pure virtual function (abstract)-
* definition with =0
* - pure virtual function can have a body
*.
* - Once one function in a class is virtual, there’s no additional
* overhead to making other functions virtual because as soon as one
* function is virtual, a virtual table is added to every object.
* Thus it doesn’t cost you much to follow the rule of making the
* destructor virtual in any class with virtual functions.
*.
* abstract class -
* a class with atleast one function pure virtual
* -cannot declare an object of a class if the class is abstract
* -can't write a function call that passes or returns an object of abstract class
* eg void invalidFunc( abstractClass ) or abstractClass invalidFunc
* because this results in creation of object and therefor can't do so
*/
<< Home