Chapter no. 6 Polymorphism
Polymorphism
Polymorphism
Compile Time
Function Overloading
Run Time
Operator Overloading
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
Virtual Functions
-1-
Chapter no. 6 Polymorphism
Function Overriding class Electronix { public: void show(int x) { cout<<"\nThis is: "<<x; } }; class Computer: public Electronix { public: void show(int x) { cout<<"\nThis is: "<<x*x; } }; int main( ) { Computer c; c.show(5); Electronix e; e.show(6); c.Electronix::show(4); return 0; }
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-2-
Chapter no. 6 Polymorphism
class Mul { public: int calculate(int m, int n) { return(m * n); } }; class Add: public Mul { public: int calculate(int x, int y) { return(x + y); } }; class Sub: public Add { public: int calculate(int a, int b) { return(a - b); } }; int main( ) { Sub s; cout<<"\nResult 1: "<<s.calculate(5, 2); cout<<"\nResult 2: "<<s.Mul::calculate(4, 3); Mul m; //cout<<"\nResult 3: "<<m.Add::calculate(2,6); return 0; } By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-3-
Chapter no. 6 Polymorphism
Overloading versus Overriding Sr. Overloading Overriding No. 1 Function name is Function name is same but parameters same and are different parameters are also same 2 Overloaded These functions Functions must be can’t contain same defined in the same scope, they are scope defined in two derived classes 3 It is the part of It is the part of run compile time time polymorphism polymorphism 4 Appropriate function Appropriate function is called as per the is called as per the arguments given object used 5 Constructor can be Constructor can’t be overloaded overridden 6 Function, Operator Only Function and constructor overriding overloading
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-4-
Chapter no. 6 Polymorphism
Virtual Function class Base { public: void show( ) { cout<<"\nBase show"; } virtual void display( ) // statement 3 { cout<<"\nBase display"; } }; class Derived: public Base { public: void show( ) { cout<<"\nDerived show"; } void display( ) { cout<<"\nDerived display"; } }; int main() { Base b, *p; Derived d; p = &b; // statement 1 p -> show(); p -> display(); p = &d; // statement 2 p -> show(); p -> display(); return 0; } By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-5-
Chapter no. 6 Polymorphism
class Engineering { protected: float result; virtual void showIt( ) { } // statement 3 public: }; class Mechanical: public Engineering { public: Mechanical(float x) { result = x; } void showIt( ) { cout<<"\nMechanical Result: "< showIt( ); ptr = &p; // statement 2 ptr -> showIt(); }
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-6-
Chapter no. 6 Polymorphism
Rules for virtual functions: 1.
Virtual function must be member of some class.
2.
They can not be static members.
3.
They are accessed by using object pointers.
4.
A virtual function can be friend of another class.
5.
A virtual function in the base class must be defined, even though it may not be used.
6.
In order to take advantage of virtual they must be overridden functions.
7.
We can’t have virtual constructors but can have virtual destructors.
8.
We can not use pointer of the derived class to access any thing from the base class.
9.
When a base pointer points to a derived class, incrementing or decrementing it will not make it to point next object of the derived class. It is incremented or decremented only relative to its base type.
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases the base function will be called.
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-7-
Chapter no. 6 Polymorphism
‘Static Binding’ versus ‘Dynamic Binding’ Sr. Static Binding No. 1 This is also referred as early / compile time binding / static linking 2 Compiler decides the action when program is compiled 3 Compiler is known to the linking of objects with different facts 4 C++ achieves this by function/operator overloading 5 Does not cause any run-time errors 6 As call is already decided this binding is fast
Dynamic Binding It is referred as late / run time binding / dynamic linking Compiler decides the action after program execution Compiler is unknown to the linking of objects with different facts C++ achieves this by the use of virtual function Possibility of runtime errors As call is not decided already this binding is slower
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-8-