15
OVERLOADED METHOD & OVERLOADED OPERATOR

OVERLOADED METHOD & OVERLOADED OPERATOR

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

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.

• 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.

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.

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...

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;

}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

}

#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.

Example

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";

}

Continue...

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();

}

 

Rules of overloaded function

The type of the parameter must be different

The amount a parameter received is different for each function.

Summary of Overloading Function

Overloaded function

Is a mechanism which allows 2 or more related function to share name but with different parameter declaration.

Is a mechanism which allows 2 or more related function to share name but with different parameter declaration.

Applies polymorphism concept

Applies polymorphism concept

The overloading function can reduce the difficulty of a program by allowing operation to be referred with the same name.

The overloading function can reduce the difficulty of a program by allowing operation to be referred with the same name.

• 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:

Overloaded Operator

Syntax Format for Operator Function

return value class-name :: operator # (list of argument)

{

// Operation to be done

}

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.

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;

}

Example (cont..)Output

2 limitations during the process of operator overloading

Precedence of the operator cannot be changed

Precedence of the operator cannot be changed

The number of operand cannot be changed

The number of operand cannot be changed

Rules in overloaded operators

Operator function cannot contain default arguments

Operators that cannot be overloaded

. :: .* ? preprosesor operator

Summary of Overloaded OperatorOverloaded

OperatorSimilar to function overloading.

When an operator is overloaded the original meaning would not lost and it will have added meaning to the class, which defines it.

Operator overloading uses operator function

General syntax for operator function

return type class-name :: operator # (list of argument)

{// body of the function

}