Operator Overloading
Operator Overloading
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:
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.
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.
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.
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;
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);
}
In the string example discussed earlier, we can convert the object string to char* as follows:
string:: operator char*( )
{
return (str) ;
}
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
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 ( ) ;
}
Stock Details-stock2-type
code 10 1
Total Value Rs. 1250