Download presentation
Presentation is loading. Please wait.
Published bySumathi G Modified over 5 years ago
1
POLYMORPHISM ( in C++) POLYMORPHISM ( in C++)
2
Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading Operator overloading Operator overriding Virtual function
3
Polymorphism – Definition Poly many, Morph form Polymorphism many forms Ability to perform different tasks or operations at different instances of time. Example: Person student in school/college child to his parents brother to his siblings etc., Single person exhibiting different forms at different instances
4
Types
5
Function Overloading Same function performing different operations or tasks Function name is same Function definitions are different The correct function is invoked by matching the corresponding function prototypes
6
Example Function calls cout<<“area of square”<<area(5); cout<<“area of rect”<<area(10,20); cout<<“area of circle”<<area(3.5); Function definitions float area (float r) {return (3.14*r*);} int area (int s) {return (s*s);} int area (int l, int b) {return (l*b);}
7
Function Overloading Contd., The compiler is aware of exactly which function to be called So function overloading achieves compile time polymorphism or Early binding
8
Operator Overloading Defining an additional task to (some of) the built-in operators Keyword: operator For unary operators no arguments For binary operators one argument
9
Operator Overloading Contd., Syntax for unary operator: returntype operator symbol() {statements;} Syntax for binary operator: returntype operator symbol(datatype arg) {statements;} Overloaded operators are invoked by: symbol x or x symbol (say –x or x – ) for unary x symbol y (x – y) for binary
10
Example unary operator (minus) class unaryopr { public: int x,y; void getdata (int a, int b) {x=a; y=b;} void display () {cout<<x<< “and”<<y;} unaryopr operator – () {x=-x; y=-y;} }; void main() {unaryopr U; U.getdata(10,-20); cout<<“nos before overloading”; U.display(); -U; cout<<“nos after overloading”; U.display(); getch(); }
11
Output: nos before overloading 10 -20 nos after overloading -10 20 Example unary operator (minus) contd.,
12
class complex //Addition of complex nos {int a,b; public: void getdata(int x, int y) {a=x; b=y;} complex operator +(complex ob) { complex t; t.a = a + ob.a; t.b = b + ob.b; return t; } void display() {cout<<a<<“+”<<b<<“i”<<“\n”;} }; Example binary operator (plus)
13
Example binary operator (plus) contd., void main() {complex obj1,obj2,res; obj1.getdata(4,5); obj2.getdata(2,2); cout<<“Input values:”<<“\n”; obj1.display(); obj2.display(); res=obj1+obj2; cout<<“Result”<<“\n”; res.display(); getch(); } Output: Input values: 4+5i 2+2i Result: 6+7i
14
Limitations of Operator Overloading Following operators cannot be overloaded sizeof operator Scope resolution operator Conditional operator Membership (dot) operator Pointer to member operator(.*)
15
Function Overriding Redefining a base class function in the derived class with same name and arguments Overridden functions are accessed using pointer referencing the objects instead of direct access through objects If not done so, the overridden function in the derived class will hide the corresponding function in the base class
16
Example class BC {public: void show() {cout<<“show base”;} }; class DC : public BC {public: void show() {cout<<“show derived”;} }; void main() {BC *bptr; BC base; bptr=&base; bptr show(); //calls BC function DC derived; bptr=&derived; bptr show(); //calls BC function } Output: show base show base
17
Virtual Function Though base class pointer points derived class object, it always executes base class function Because the compiler ignores the contents of pointer and chooses the function that matches the type of the pointer If so, then how polymorphism is achieved? It is achieved using virtual functions Keyword virtual is used before the function in the base class
18
Virtual Function Contd., By doing so, the function is called based on the object referenced by the pointer instead of pointer type at run time The keyword virtual indicates the compiler to perform late binding (run time) on that function Hence run time polymorphism is achieved
19
Example class BC {public: virtual void show() {cout<<“show base”;} }; class DC : public BC {public: void show() {cout<<“show derived”;} }; void main() {BC *bptr; BC base; bptr=&base; bptr show(); //calls BC function DC derived; bptr=&derived; bptr show(); //calls DC function } Output: show base show derived
20
Thank you
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.