0% found this document useful (0 votes)
11 views

Operator Overloading

Operator Overloading

Uploaded by

drsomeshdewangan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Operator Overloading

Operator Overloading

Uploaded by

drsomeshdewangan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LECTURE-22 & 23

OPERATOR OVERLOADING:-
Operator overloading provides a flexible option for the creation of new definations for most
of the C++ operators. We can overload all the C++ operators except the following:

Class members access operator (. , .*)


Scope resolution operator (: :)
Size operator(sizeof)
Condition operator (? :)
Although the semantics of an operator can be extended, we can't change its syntax, the
grammatical rules that govern its use such as the no of operands precedence and associativety. For
example the multiplication operator will enjoy higher precedence than the addition operator.
When an operator is overloaded, its original meaning is not lost. For example,
the operator +, which has been overloaded to add two vectors, can still be used to add two integers.

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.
Syntax:-
return-type class-name :: operator op( arg-list)
{
function body
}
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 function, or friend
function. A basic defference between them is that a friend function will have only one argument for
unary operators and two for binary operators, This is because the object used to invoke the member
function is passed implicitly and therefore is available for the member functions. Arguments may be
either by value or by reference.

operator functions are declared in. the class using prototypes as follows:-
vector operator + (vector); /./ vector addition
vector operator-( ); //unary minus
friend vector operator + (vuelor, vector); // vector add
friend vector operator -(vector); // unary minus
vector operator - ( vector &a); // substraction
int operator = =(vector); //comparision
friend int operator = =(vector ,vrctor); // comparision
vector is a data type of class and may represent both magnitude and direction or a series
of points called elements.
The process of overloading involves the following steps:-
1. Create a class that defines the data type that is used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class
3. It may be either a member function or friend function.
4. Define the operator function to implement the required operations.
Overloaded operator functions can be invoked by expressions such as
op x or x op;
for unary operators and
x op y
for binary opearators.
operator op(x);
for unary operator using friend function
operator op(x,y);
for binary operator usinf friend function.

Unary operator overloading(using member function):


class abc
{
int m,n;
public:
abc()
{
m=8;
n=9;
}
void show()
{
cout<<m<<n;
}
operator -- ()
{
--m;
--n;
}
};
void main()
{
abc x;
x.show();
--x;
x.show();
}

Unary - operator overloading(using friend function):


class abc
{
int m,n;
public:
abc()
{
m=8;
n=9;
}
void show()
{
cout<<m<<n;
}
friend operator --(abc &p);
};
operator -- (abc &p)
{
--p.m;
--p.n;
}
};
void main()
{
abc x;
x.show();
operator--(x);
x.show();
}
Unary operator+ for adding two complex numbers (using member function)

class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float r,float i)
{
real=r;
img=i;
}
void show()
{
<<img;
}
complex operator+(complex &p)
{
complex w;
w.real=real+q.real;
w.img=img+q.img;
return w;
}
};
void main()
{
complex s(3,4);
complex t(4,5);
complex m;
m=s+t;
s.show();
t.show();
m.show();
}
Unary operator+ for adding two complex numbers (using friend function)
class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float r,float i)
{
real=r;
img=i;
}
void show()
{
cout<<real<
}
friend complex operator+(complex &p,complex &q);
};
complex operator+(complex &p,complex &q)
{
complex w;
w.real=p.real+q.real;
w.img=p.img+q.img;
return w;
}
};
void main()
{
complex s(3,4);complex t(4,5);
complex m;
m=operator+(s,t);
s.show();t.show();
m.show();
}

Overloading an operator does not change its basic meaning. For example assume the +
operator can be overloaded to subtract two objects. But the code becomes unreachable.
class integer
{
intx, y;
public:
int operator + ( ) ;
}
int integer: : operator + ( )
{
return (x-y) ;
}
Unary operators, overloaded by means of a member function, take no explicit argument and
return no explicit values. But, those overloaded by means of a friend function take one
reference argument (the object of the relevant class).
Binary operators overloaded through a member function take one explicit argument and those
which are overloaded through a friend function take two explicit arguments.

Table 7.2
Operator to Arguments passed to the Arguments passed to the Friend
Overload Member Function Function
Unary Operator No 1
Binary Operator 1 2
LECTURE-24

Type Conversions
In a mixed expression constants and variables are of different data types. The assignment operations
causes automatic type conversion between the operand as per certain rules.

The type of data to the right of an assignment operator is automatically converted to the data type of
variable on the left.

Consider the following example:


int x;
float y = 20.123;
x=y ;

This converts float variable y to an integer before its value assigned to x. The type conversion is
automatic as far as data types involved are built in types. We can also use the assignment operator in
case of objects to copy values of all data members of right hand object to the object on left hand. The
objects in this case are of same data type. But of objects are of different data types we must apply
conversion rules for assignment.

There are three types of situations that arise where data conversion are between incompatible types.
1. Conversion from built in type to class type.
2. Conversion from class type to built in type.
3. Conversion from one class type to another.

Basic to Class Type


A constructor was used to build a matrix object from an int type array. Similarly, we used another
constructor to build a string type object from a char* type variable. In these examples constructors
performed a defacto type conversion from the argument's type to the constructor's class type

Consider the following constructor:

string :: string (char*a)


{
length = strlen (a);
name=new char[len+1];
strcpy (name,a);
}

This constructor builds a string type object from a char* type variable a. The variables length and
name are data members of the class string. Once you define the
constructor in the class string, it can be used for conversion from char* type to string type.

Example
string si , s2;

s1 = string(namel);
s2 = name2;
The program statement

si = string (namel);

first converts name 1 from char* type to string type and then assigns the string type values to the
object s1. The statement

s2 = name2;

performs the same job by invoking the constructor implicitly.


Consider the following example
class time
{
int hours;
int minutes;
public:
time (int t) // constructor
{
hours = t / 60; //t is inputted in minutes
minutes = t % 60;
}
};

In the following conversion statements :

time Tl; //object Tl created


int period = 160;
Tl = period; //int to class type
The object Tl is created. The variable period of data type integer is converted into class type time by
invoking the constructor. After this conversion, the data member hours ofTl will have value 2 arid
minutes will have a value of 40 denoting 2 hours and 40 minutes.

Note that the constructors used for the type conversion take a single argument whose type is to be
converted.

In both the examples, the left-hand operand of = operator is always a class object. Hence, we can
also accomplish this conversion using an overloaded = operator.
LECTURE-25
Class to Basic Type
The constructor functions do not support conversion from a class to basic type. C++ allows us to
define a overloaded casting operator that convert a class type data to basic type. The general form of
an overloaded casting operator function, also referred to as a conversion function, is:
operator typename ( )
{
//Program statmerit .
}

This function converts a class type data to typename. For example, the operator double( ) converts a
class object to type double, in the following conversion function:
vector:: operator double ( )
{
double sum = 0 ;
for(int I = 0; ioize;
sum = sum + v[i] * v[i ] ; //scalar magnitude
return sqrt(sum);
}

The casting operator should satisfy the following conditions.


It must be a class member.
It must not specify a return type.
It must not have any arguments. Since it is a member function, it is invoked
by the object and therefore, the values used for, Conversion inside the
function belongs to the object that invoked the function. As a result function
does not need an argument.

In the string example discussed earlier, we can convert the object string to char* as follows:
string:: operator char*( )
{
return (str) ;
}

One Class to Another Class Type


We have just seen data conversion techniques from a basic to class type and a class to basic type. But
sometimes we would like to convert one class data type to another class type.

Example
Obj1 = Obj2 ; //Obj1 and Obj2 are objects of different classes.
Objl is an object of class one and Obj2 is an object of class two. The class two type data is converted
to class one type data and the converted value is assigned to the Objl. Since the conversion takes
place from class two to class one, two is known as the source and one is known as the destination
class.
Such conversion between objects of different classes can be carried out by either a
constructor or a conversion function. Which form to use, depends upon where we want the type-
conversion function to be located, whether in the source class or in the destination class.
We studied that the casting operator function
Operator typename( )
Converts the class object of which it is a member to typename. The type name may be a built-in type
or a user defined one(another class type) . In the case of conversions between objects,
typename refers to the destination class. Therefore, when a class needs to be converted, a
casting operator function can be used. The conversion takes place in the source class and the result is
given to the destination class object.
Let us consider a single-argument constructor function which serves as an instruction for
converting the argument's type to the class type of which it is a member. The argument belongs to
the source class and is passed to the destination class for conversion. Therefore the conversion
constructor must be placed in the destination class.
Table 7.3

Conversion Conversion takes place in


Source class Destination class
Basic to class Not applicable Constructor
Class to Basic Casting operator Not applicable
Class to class Casting operator Constructor

When a conversion using a constructor is performed in the destination class, we must be able to
access the data members of the object sent (by the source class) as an argument. Since data members
of the source class are private, we must use special access functions in the source class to facilitate
its data flow to the destination class.

Consider the following example of an inventory of products in a store. One way of keeping record of
the details of the products is to record their code number, total items in the stock and the cost of each
item. Alternatively we could just specify the item code and the value of the item in the stock. The
following program uses classes and shows how to convert data of one type to another.

#include<iostream.h>
#include<conio.h>
class stock2;
class stock1
{
int code, item;
float price;
public:
stockl (int a, int b, float c)
{
code=a;
item=b;
price=c;
}
void disp( )
{
\
\
\
}
int getcode( )
{return code; }
int getitem( )
{return item; }
int getprice( )
{return price;}
operator float( )
{
return ( item*price );
}
};

class stock2
{
int code;
float val;
public:
stock2()
{
code=0; val=0;
}
stock2(int x, float y)
{
code=x; val=y;
}
void disp( )
{
\
\
}
stock2 (stockl p)
{
code=p . getcode ( ) ;
val=p.getitem( ) * p. getprice ( ) ;
}
};

void main ( )
{ '
Stockl il(101, 10,125.0);
Stock2 12;
float tot_val;
tot_val=i1 ;
i2=il ;
-stockl- \
i 1 . disp ( ) ;
\
\
-stock2- \
i2 .disp( ) ;
getch ( ) ;
}

You should get the following output.


Stock Details-stock1-type
code 101
Items 10
Price per item Rs. 125
Stock value
1250

Stock Details-stock2-type
code 10 1
Total Value Rs. 1250

You might also like