Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading.

Similar presentations


Presentation on theme: "Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading."— Presentation transcript:

1 Text Overloading Techniques –Using CPP

2 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading 4.Operator Overloading -Unary Operator Overloading -Binary Operator Overloading Q & A

3 3 Overloading What is Overloading? –Providing additional behaviour in terms of Logic for the existing behaviour –Ex:Lets Assume there is CalculateSum() behaviour exists for 2 No's –Why cant you make the Same function(Behaviour) to work on the three Nos,Four Numbers or No of Arguments of different types –Advantage: Readability and Easy Maintanance As Client Program / Developer no need to remember different behaviour Names / function names to use in their code They can always use same function name but with difference in arguments in terms of No of arguments,their types or order of the arguments.

4 4 One or More Function with a Same Name with different logic,which is based on the Function signature –So logic varies with different function signature –Function Signature: –Combination of Function Name,Total Number of Arguments,Data Type of the each Arguments and Order of the Arguments. int sum(int a, int b); --->1 int incr() -->2 int sum(); --->3 int sum(float a, float b); --->4 int sum(int a, float b); --->5 int sum(float a, int b) ; --->6 int sum(int a, float b, char* name)-->7 int sum(float a, int b, char* name)-->8 int sum(char* name, float a,int b)-->9 Obervation on above functions: –Check 1 and 2--Signature differ in function name -Not Overloading –1 and 3 -->Overloaded with different Signature on No of arguments. –1 and 4, 1 and 5,1 and 6 -->Overloaded with different Signature on Data Type –5 and 6 --->Overloaded with different Signature on Order of Parameters Function Overloading

5 Constructor Overloading Similar to function overloading but parameterized constructor gets overloaded –Means One or More Constroctors with a Same Name with different logic,which is based on the Constructor signature interms of Arguements. Ex: class Student{ int id; string name,std; Student(){ id=0; name=”ILP” } Student(int lid, string lname){ id=ild; name=lname; } Student(int lid,string lname,string lstd){ id=ild; name=lname; std=lstd; } } int main() { Student stdObj; Student stdObj1(1,"Kalam"); // Calls Constructor two parameters Student stdObj2(2,"Raman","Fifth");// Calls Constructor with three //parameters }

6 6 Providing additional behaviour / meaning for the existing / supported operators in C++ Why to Provide Additional Meaning? –Lets Assume an Example,if you want to add two integers, Two decimal bumbers OR one decimal and one integer number To perform above operation, C++ supports predefined “+” Operator –But What if, if I want to perform addition operation On Complex Numbers Sum of balance amount from Two Bank Accounts Total Credit scores of a student from various Compututive exam –So Here the default behaviour of “ + ”provided by C++ compiler will not sufficient, As is works on only the variables of predefined data types –But here Complex No,Bank Account and Competutive Exam are the user defined types –So How to Make the “ + “ operator to work on User defined types –To make the operations like these to work on User defined objects, Operator overloading is introduced. Operator Overloading

7 7 Let us Summarize the Concept : Operator Overloading is nothing but defining the user defined logic for the existing/supported operators so as to make these operators to work on the even user defined Data types. So Providing addition meaning of working on user defineds types inaddition to the existing behaviour of working on predefined data types. Overload all of the Operators supported except This,sizeof,*,*-> operators like Syntax: rutuentype Operator (Objects as Arguements) Ex: Complex Operator +(Complex obj2); Operator Overloading

8 8 Its nothing but overloading the binary operators like “+”, ”-”,”/” etc.., Various ways to Overload to binary Operator: –One as Member Function of the Class –Other one as Friend Function So If we overload Binary Operator as Member function, we would require to pass Second Operand(Object ) as a parameter –Because First Operand(Object) can be accessed implecitely as the function is a member and is called using the first operand itself Ex: “+” as Member Function Counter Counter:: operator + (Counter C1) { Counter temp; temp.x = x+ C1.x; temp.y = y + C1.y; return temp; } int main() { Counter C1,C2,C3; C3 = C1 + C2; // Call converted to “C1.+(C2) “,Which is not Matching with the function def C3.display(); } Operator Overloading -Binary Operators

9 Binary Operator overloading using Friend function : -Required Two arguments would be passed as the function is called without the Object, so No object gets passed implecitely Example: Counter operator + (Counter ); //member function declaration for Binary + operator friend Counter operator + ( Counter, Counter); // Friend declaration for binary + operator Redefinition for friend would look like: Counter operator + (Counter C1,Counter C2) { Counter temp; temp.x = C1.x+ C2.x; temp.y = C1.y + C2.y; return temp; } Operator Overloading -Binary Operators

10 10 Its nothing but overloading the unary operators like “++(Pre)”,”(Post)++”,“-” etc.., Various ways to Overload to binary Operator: –One as Member Function of the Class –Other one as Friend Function So If we overload Unary Operator as Member function, No need to pass the Operand(Object ) as a parameter Because the Operand(Object) can be accessed implecitely as the function is a member and is called using the operand (Object) itself Ex: “++” as Member Function Counter operator ++() { Counter temp; temp.count = ++count; return temp; } int main() { Counter C1,C2; C2=++C1; //// Call converted to “C1.++() “,Which is not Matching with the function def } Operator Overloading -Unary Operators

11 11 Operator Overloading -Unary Operator UNARY Operator overloading using Friend function : -One arguments would be passed as the function is called without the Object, so No object gets passed implecitely Example: Counter operator - ( ); //member function declaration for Unary - operator friend Counter operator - ( Counter); // Friend declaration for Unary - operator Redefinition for friend would look like: Counter operator - (Counter C1) { Counter temp; temp.x = - C1.x; return temp; } int main() { Counter C1; C2=-C1; //// Call converted to “-(C1) “,Which is not Matching with the function def

12 Text Thank you


Download ppt "Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading."

Similar presentations


Ads by Google