1 / 15

OVERLOADED METHOD & OVERLOADED OPERATOR

OVERLOADED METHOD & OVERLOADED OPERATOR. Definition Overloaded Method. Overloading function is a mechanism; which allows 2 or more related function to share a same name but the declaration of parameter functions is different.

gay-keller
Download Presentation

OVERLOADED METHOD & OVERLOADED OPERATOR

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OVERLOADED METHOD & OVERLOADED OPERATOR

  2. Definition Overloaded Method • Overloading function is a mechanism; which allows 2 or more related function to share a same name but the declaration of parameter functions is different. • In this situation, the shared name of a function is called overload and this process is referred as a function which shows overloading • The function overloading is a method of C++ that applies polymorphism concept during compiling.

  3. Definition Overloaded Method(2) • The overloading function can reduce the difficulty of a program by allowing operation to be referred with the same name. • How to create overloading function? -Declare all versions of the functions. -The compiler automatically with select the most suitable version of the overloaded function based on the argument used to call the function.

  4. Definition Overloaded Method(2) • The example below shows how the function name set() is used with 5 different versions: class ABC { int a; char ab [10]; double aks; float price; public: void set (int x) ;//method 1 void set (char xy[10] ); //method 2 void set (double h); //method 3 void set(float p); //method 4 void set ( int x, float y ) ; //method 5 }; Continue...

  5. Continue... void main ( ) { ABC object1; object1.set(6); //calling method 1 object1.set (“SIX”); //calling method 2 object1.set (3.00); // calling method 3 object1.set (7.5); // calling method 4 object1.set (9,8.96); // calling method 5 } void ABC::set(int x) //method 1 { a=x; } void ABC::set(char xy[10]) //method 2 { strcpy(ab,xy); } void ABC::set(double h) //method 3 { aks=h; } void ABC::set(float p) //method 4 { price =p; } void ABC::set(int x, float y ) //method 5 { a=x; price = y; }

  6. Example #include<iostream.h> #include<string.h>  class Student{ char name[30]; int age; char ic[30]; public: void setdata(char *a, char * c); void setdata(int b); void showdata(); }; There are 2 functions called setdata but for both the function it receives amount and different types of parameter.

  7. void main() { char name[30]; int age; char ic[30]; cout<<"Enter your name:"; cin.getline(name,30); cout<<"Enter your ic:"; cin.getline(ic,30); cout<<"Enter your age:"; cin>>age; Student a; a.setdata(name,ic); a.setdata(age); a.showdata(); } Continue... void student::setdata(char *a,char * c) { strcpy(name,a); strcpy(ic,c); } void student::setdata(int b) { age=b; } void student::showdata(); { cout<<"Name:"<<name<<"\n"; cout<<"IC:"<<ic<<"\n"; cout<<"Age:"<<age<<"\n"; }

  8. The amount a parameter received is different for each function. Rules of overloaded function • The type of the parameter must be different

  9. Summary of Overloading Function Overloaded function Is a mechanism which allows 2 or more related function to share name but with different parameter declaration. The overloading function can reduce the difficulty of a program by allowing operation to be referred with the same name. Applies polymorphism concept

  10. Overloaded Operator • Operator overloading and function overloading is similar. • According to the facts, the operator overloading is actually part of function overloading. • When an operator is overload, the original meaning of the operator is not lost. Besides that, it will have added meaning to the class, which defines it. • Overloaded operator uses overloading function:

  11. Syntax Format for Operator Function return value class-name :: operator# (list of argument) { // Operation to be done } • return value – normally this function returns the type of declared class (even though the free operator function can return any types of data) • The symbol # is placement for the operators that will be overloaded (example: sign +,++,-,*). • The list of argument - depends on how the overloading function is done.

  12. Example #include <iostream.h> class numbers { int x,y,z; public: void set_data(int,int,int); void show_data(); void operator-(); /*overload operator unari*/ }; void numbers::set_data(int a,int b,int c) { x=a; y=b; z=c; } void numbers::show_data() { cout<<"\n x="<<x; cout<<"\n y="<<y; cout<<"\n z="<<z; } //definition of operator –() void numbers::operator-() { x=-x; y=-y; z=-z; } void main() { numbers num;//creating object num.set_data(11,-22,33); cout<<"\n The numbers are:"; num.show_data(); -num;//calling operator –() cout<<"\n The numbers are:"; num.show_data(); cout<<endl; }

  13. Output Example (cont..)

  14. 2 limitations during the process of operator overloading Precedence of the operator cannot be changed The number of operand cannot be changed Operator function cannot contain default arguments Rules in overloaded operators Operators that cannot be overloaded . :: .* ? preprosesor operator

  15. Summary of Overloaded Operator Similar to function overloading. Overloaded Operator When an operator is overloaded the original meaning would not lost and it will have added meaning to the class, which defines it. return type class-name :: operator# (list of argument) { // body of the function } Operator overloading uses operator function General syntax for operator function

More Related