Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.

Similar presentations


Presentation on theme: "Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism."— Presentation transcript:

1 Unit VI polymorphism

2 Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism (static binding)  Run time polymorphism (dynamic binding) The compile time polymorphism (early binding) is achieved through function and operator overloading. The dynamic (late) binding is achieved through virtual functions.

3 Md.Jaffar Sadiqsumalathasumalatha Function overloading Overloading refers to the use of the same thing for different purposes. Function overloading: Same function name is used to perform different tasks. (same function name with different signatures) In c++, the functions may have same name with different argument list, then the functions are said to be overloaded. The parameter list is different in terms of:  The types of the parameters  The number of arguments  The order of the arguments in the list

4 Md.Jaffar Sadiqsumalathasumalatha Ex: An overloaded abc() function handles different data types of data: function prototypes : 1. void abc(); 2.void abc(int); 3.void abc(double); 4.void abc(int, int); 5.int abc(int, float); The function calls : abc(); abc(10,20.0); abc(10.0); abc(30); abc( 10, 10); Calls 1. prototype Calls 5. prototype Calls 3. prototype Calls 2. prototype Calls 4. prototype

5 Md.Jaffar Sadiqsumalathasumalatha A function call first matches the prototype having same number and type of arguments and then calls the appropriate function for execution. The correct match must be unique. Ex: void sum( int,int); Int sum(int, int); In this case the sum() is not said to be overloaded, because the signatures of these functions are same. And when we call the function sum(10,10), Compiler will generate an error.

6 Md.Jaffar Sadiqsumalathasumalatha Friend functions A friend function is a non member function to any class,even though it has capability to access the private data of any class. In order to make an outside function is friendly to a class, we need to use a keyword called friend before a function declaration. Ex: Class ABC { ……… public: friend function _name (arguments); }; Return_ type function _name( arguments) { …………} Main() { ……… function_name(arg); } Friend function declaration Friend function definition Friend function call

7 Md.Jaffar Sadiqsumalathasumalatha Characteristics of friend function Friend functions are non member functions. So it can not access the private members directly and has to use an object name and dot operator with each member. Class sample { int x ; public : sample() { x=0 ;} friend void update( sample); void show(); }; void update( sample s) { s.x++; } Void sample :: show() { cout << x ; } Main() { sample s1; update(s1); s1.show(); }

8 Md.Jaffar Sadiqsumalathasumalatha Member functions of one class can be friend functions of another class. Class A { public: int fun1(); }; Class B { public: friend int A :: fun1(); }; An non member function may access the data from more than one class with the help of friend Class ABC { …………. friend void show( ABC,XYZ); …….. }; Class XYZ { ……….. friend void show (ABC,XYZ); …….. };

9 Md.Jaffar Sadiqsumalatha #include Using namespace std; class XYZ; // forward declaration class ABC { int a; public: ABC() {a=9;} friend void max(ABC,XYZ); }; class XYZ { int b; public: XYZ() { b=10} friend void max(ABC,XYZ); }; Void max( ABC A, XYZ X) { if (A.a > X.b) cout<< A. a<< “is greater” ; else cout<<X.b<<“is greater”; } Main() { ABC A; XYZ X ; max(A,X); }

10 Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } void sum (complex,complex ); }; Void complex:: sum(complex c1, complex c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; cout<<"The sum is:" <<c.x<<"+i"<<c.y; } main() { complex c1(10,10); complex c2(20,20); complex c3; c3.sum(c1,c2); } Addition of two Complex numbers

11 Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } complex (complex &,complex &); }; complex:: complex(complex &c1, complex &c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; cout<<"The sum is:" <<c.x<<"+i"<<c.y; } main() { complex c1(10,10); complex c2(20,20); complex c3(c1,c2); } Addition of two complex numbers using copy constructor

12 Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } friend complex sum (complex,complex ); Void display(); }; Complex sum(complex c1, complex c2) { complex c ; c.x= a.x+b.x; c.y=a.x+b.y; return c; } void complex ::display() { cout <<x<<“+”<<y; } main() { complex c1(10,10); complex c2(20,20); complex c3; c3=sum(c1,c2); c3.display(); } Addition of two complex numbers using friend function

13 Md.Jaffar Sadiqsumalatha Operator overloading C++ permits us to provide an additional meaning to existing operators. Using operator overloading, we can add two user defined objects with the same syntax that is applied to the basic types Ex: a+b ( a, b are integers) (a+b) ( a, b are two user defined data types ),it is possible using operator overloading) –Examples of already overloaded operators Operator << is both the stream-insertion operator and the bitwise left-shift operator

14 Md.Jaffar Sadiqsumalatha Overloadable operators All operators can be overloaded except the following operators operator category operators member access dot operator scope resolution :: conditional ?: pointer to member * size of data type sizeof()

15 Md.Jaffar Sadiqsumalatha Rules for overloading operators Overloading restrictions –Precedence of an operator cannot be changed –Associativity of an operator cannot be changed –Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created –Use only existing operators No overloading operators for built-in types

16 Md.Jaffar Sadiqsumalatha Defining operator overloading The operator function is used to define the additional task to an operator. The general form of an operator function is: Return type class name::operator op (arg) {………. } where op is the operator being overloaded. Ex: operator+ used to overload the addition operator (+). The operator function must be either member function or friend function sumalatha

17 Unary Operator Overloading -Unary operator takes only one operand. -Unary operators are unary -, ++, - -, etc., For example a minus operator takes one operand (-m) We know that this operator changes the sign of an operand when applied to a basic data item. Example Program void main() { int m=100; m=-m; cout<<m; }

18 Md.Jaffar Sadiqsumalatha Overloading unary minus operators sumalatha Class sample { int x, y ; Public: sample(int a, int b) { x=a;y=b; } void operator-(); Void display(); }; Void sample::operator-() { x=-x; y=-y; } Void display() { cout<<x<<y; } Main() { sample S(3,4); S.display(); -S; // calls operator-() function s.display(); }

19 Md.Jaffar Sadiqsumalatha Overloading unary minus operator using friend function Class sample { int x, y ; Public: sample(int a, int b) { x=a;y=b; } friend void operator-(sample &); Void display(); }; Void operator-(sample & s) { s.x=-s.x; s.y=-s.y; } Void display() { cout<<x<<y; } Main() { sample S(3,4); S.display(); operator–(S); // calls operator-() function s.display(); }

20 Binary Operator Overloading -Binary operator takes two operands. -Binary operators are +, -, *, etc., For example a plus operator takes two operands (a + b) Example Program void main() { int a=100,b=200,c; c=a+b; cout<<c; }

21 Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } complex operator+ (complex ); void display(); }; complex complex:: operator+ ( complex c2) { complex c ; c.x= x+c2.x; c.y=y+c2.y; return c; } Void complex :: display() { cout<<x<<“+”<<y;} main() { complex c1(10,10); complex c2(20,20); complex c3; c3=c1+c2 // c3.operaor+(c1,c2); c3.display(); } Overloading binary operators

22 Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } friend complex operator+ (complex,complex); void display(); }; complex complex:: operator+ (complex c1, complex c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; return c; } Void complex :: display() { cout<<x<<“+”<<y;} main() { complex c1(10,10); complex c2(20,20); complex c3; c3.operaor+(c1,c2); c3.display(); } Overloading binary operators using friend function

23 Md.Jaffar Sadiqsumalatha Operator function can be declared as member function or using friend function. We cannot use friend functions to overload certain operators. However, member functions can be used to overload them operator category operators Assignment operator= Function call operator() Subscripting operator[] Class member access operator -> There are certain situations where we will use only friend function rather than member function. Consider a statement A= 2 + B where A,B are two objects of same class 2 is integer type. Here we can’t use member function because the left hand operand must be an object. A =2.operator+(B) is illegal But this can be achieved through friend function i.e : A=operator+ ( 2, B)


Download ppt "Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism."

Similar presentations


Ads by Google