0% found this document useful (0 votes)
17 views10 pages

Operatore Overloading1

Uploaded by

renukathakre80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Operatore Overloading1

Uploaded by

renukathakre80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Operator Overloading

C++ tries to make the user-defined data types behave in much the same way the built-in types.
For instance. C++ permits us to add two variables of user-defined types with the same syntax
that is applied to the basic types. This means that C++ has the
ability to provide the operators with a special meaning for a data type. The
mechanism of giving such special meanings to an operator is known as operator
overloading. Operator overloading provides a flexible option for the creation of new definitions
for most of the C++ operators.
We can overload all the C++ operators except the following:

• Class member access operators (., .*).


• Scope resolution operator (::).
• Size operator (sizeof).
• Conditional operator (?:).

Defining Operator Overloading


To define an additional task to an operator, we must specify what it means in relation
to the class to which the operator is applied. This is done with the help of a special
function, called operator function, which describes the task. The general form of an
operator function is:
return type classname : : operator op(arglist)
(
Function body // task defined
}
where return type is the type of value returned by the specified operation and op is
the operator being overloaded. The op is preceded by the keyword operator,
operator op is the function name.
Operator functions must be either member functions or friend functions. A basic
difference between them is that a fiend function will have only one argument for
unary operators and two for binary operators, while a member function has no
arguments for unary operators and only one for binary operators. To explain easier it
is shown in the form of table.
Simple Function Friend Function
Unary Operator No Argument Single Argument
Binary Operator One Argument Two Arguments

This is because the object used to invoke the member function is passed implicitly
and therefore is available for the member function. This is not the case with friend
functions.

The process of overloading involves the following steps:


1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function operator op() in the public part of the class. If
may be either a member function or a friend function.

1|Page
Operator Overloading

3. Define the operator function to implement the required operations.

Overloading Unary Operators


We’ll consider the unary minus operator as unary operator to overload. A
minus operator when used as a unary, takes just one operand. We know this
operator changes then sign of and operand when applied to basic data. The
following example shows how the unary operator minus is overloaded.
Example :
class sample:
{
int a,b,c;
public:
void getdata(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
void operator –()
{
a=-a;
b=-b;
c=-c;
}
void putdata()
{
cout<<a<<b<<c;
}
};
void main()
{
sample s;
s.getdata(25,-30,-40);
-s;
s.putdata();
}

OUTPUT
-25 30 40

Another example is here to overload unary operator increment i.e. ++ which


will increase the value of variable by one.

2|Page
Operator Overloading

class sample
{
int count;
public:
sample() //constructor is used to initialize the value of count
{
count=0;
}
void operator++()
{
++count;
}
void putdata()
{
cout<<count;
}
};
void main()
{
sample s;
s++;
s.putdata();
}
OUTPUT
1

Binary Operator Overloading


To overload the binary operator using simple member function only one argument is required. It should
be object as argument. Following example shows the overloading of binary operator + which will add
the member of two different objects of the same class.

class sample
{
int x;
public:
sample()
{
}
sample(int a)
{
x=a;
}
sample operator+(sample s2)
{
3|Page
Operator Overloading

sample t;
t.x=x+s2.x;
return(t);
}
void display()
{
cout<<x;
}
};
void main()
{
sample s1(10);
sample s2(20);
sample s3=s1+s2;
s3.display();
}

OUTPUT
30
Let us have a close look at the function operator+() and see how the operator overloading
is implemented.
sample operator+(sample s2)
{
sample t;
t.x=x+s2.x;
return(t);
}

We should note the following features of this function:

1. It receives only one sample type argument explicitly.


2. It returns a sample type value.
3. It is a member function of sample.
The function is expected to add two integer values and return the sum of both as a result but receives
only one value as argument. It requires only one value as argument. We know sample s3=s1+s2;
Invokes the operator +() function.
We know that a member function can be invoked only by an object of the same class. Here, the object
s1 takes the responsibility of invoking the function and s2 plays the role of argument passed to the
function. It means the above statement has the following meaning using usual function call syntax
s3=s1.operator+(s2); Therefore, in the operator+() function, the data members of s1 are
accessed directly and the data members of s2 are accessed using the dot operator.
In the statement t.x=x+s2.x; s2.x refers to the object s2 and x refers to the object s1. t.x has been
created specially to hold the results of addition of s1 and s2.

4|Page
Operator Overloading

Overloading Binary Operator using Friend Function


We know friend function can be used to overload binary operator in place of member function.
But if you want to overload binary operator using friend function we requires two arguments to
be explicitly passed to it, while a member function requires only one.
Now we’ll revise the above example using friend function.
class sample
{
int x;
public:
sample()
{
}
sample(int a)
{
x=a;
}
friend sample operator+(sample s1, sample s2)
{
sample t;
t.x=s1.x+s2.x;
return(t);
}
void display()
{
cout<<x;
}
};
void main()
{
sample s1(10);
sample s2(20);
sample s3=s1+s2;
s3.display();
}

In this case the statement sample s3=s1+s2; is equivalent to sample s3=operator+(s1,s2);


It means you can use both the member function and friend function to overload the binary
operator. But it is compulsory to use a friend function in the situations where we need to use
two different types of operand for a binary operator say one an object and another a built in
data type. In such situation member function does not work. We must use the friend function
in these type of situations.

5|Page
Operator Overloading

Rules for Overloading Operators


1. Only existing operators can be overloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user-defined
type.
3. We cannot change the basic meaning of an operator. That is to say, we cannot
redefine the plus( + ) operator to subtract one value from the other.
4. Overloaded operators follow the syntax rules of the original operators. They cannot
be overridden.
5. There are some operators that cannot be overloaded which are listed below

6. We cannot use friend functions to overload certain operators. However, member


functions can be used to overload them. They are listed below:

7. Unary operators, overloaded by means of a member function, take no explicit


arguments and return no explicit values, but, those overloaded by means of a
friend function, take one argument.
8. Binary operators overloaded through a member function take one explicit
argument and those which are overloaded through a fiend function take
two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand
operand must be an object of the relevant class.
10. Binary arithmetic operators such as +. -. *, and / must explicitly return a value.
They must not attempt to change their own arguments.

6|Page
Operator Overloading

Type Conversions
Three types of situations might arise in the data conversion between incompatible types:

1. Conversion from basic type to class type.


2. Conversion from class type to basic type.
3. Conversion from one class type to another class type.

Basic to Class Type


The conversion from basic type to class type is easy to accomplish. We can achieve this
by using the parameterized constructor. As shown in the below example the integer type
of data is assigned to object.
class sample
{
int a;
public:
sample(int x)
{
a=x;
}
void display()
{
cout<<a;
}
};
void main()
{
int m=25;
sample s=m;
s.putdata();
}
OUTPUT
25

In the example integer type data i.e. the value of m is assigned to object s. this is achieved using
parameterized constructor. In basic to class type conversion the left hand operand of =
operator is always a class object. Therefore, we can also accomplish this conversion using an
overloaded = operator.

Class to Basic Type


C++ allows us to define an overloaded casting operator that could be used to convert a class
type data to a basic type. The general form of an overloaded casting operator function.
usually referred to as a conversion function is given below:
operator typename()

7|Page
Operator Overloading

(
(Function statements)

This function converts a class type data to type name. This casting operator function
should satisfy the following conditions:
 It must be a class member.
 It must not specify the return type.
 It must not have any arguments.
In the given below example the float type data is converted to integer by using the
conversion function.
class sample
{
float a;
public:
sample(float x)
{
a=x;
}
operator int() //conversion function
{
return((int)a);
}
void display()
{
cout<<a;
}
};
void main()
{
sample s(3.1423);
cout<<(int)s; //conversion function called here which convers float type data to
int
}
OUTPUT
3

ONE CLASS TO ANOTHER CLASS TYPE


In some situations we would like to convert one class type to another class type.
Example:
objx=objy ;
objx is an object of class x and objy is the object of class y. The class y type data is converted to
the class x type data and the converted value is assigned to the objx. Since the conversion takes

8|Page
Operator Overloading

place from class y to class x, the class y is called source class and class y is called as destination
class.
Such conversions between objects of different classes can be carried out by either a constructor
or conversion function.
Here is the example of converting one class to another class.
class sample1
{
int code;
public:
sample1(int a)
{
code=a;
}
int getcode()
{
return(code);
}
};
class sample2
{
int code;
public:
sample2() //value of code of sample2 is initialized to zero
{
code=0;
}
sample2(sample1 x) // this constructor assigns the value of code of class sample1 to code of
{ // sample2
code=x.getcode();
}
void putdata()
{
cout<<code;
}
};
void main()
{
sample s1(10);
sample s2;
s2=s1;
s2.putdata();
}
OUTPUT
10

9|Page
Operator Overloading

The constructor sample2(sample1) is used in the class sample2 to convert the sample1 type
data to the sample2 data type.

Following table provides summary of all three conversions. It shows that conversion from a
class to any other type also the class type should use a casting operator in source class. On the
other hand, to perform the conversion from any other type or class to a class type, a
constructor should be used in the destination class.

10 | P a g e

You might also like