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

unit3-chapter2

Uploaded by

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

unit3-chapter2

Uploaded by

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

UNIT 3

CHAPTER 2
OPERATOR OVERLOADDING AND TYPE CONVERSIONS
INTRODUCTION
Operator overloading is one of the features of c++ language. The process of defining an additional task to a
predefined operator is called operator overloading.
All the C++ operators can be overloaded except the following
i) Class member operator(. , .*)
ii) Scope resolution operator(::)
iii) Sizeof operator
iv) Conditional Operator(?:)
Rules:
i) Only those operators that are predefined in the C++ complier can be overloaded
ii) User cannot create new operators like @,$ etc
iii) Overloading an operator gives a meaning which is basically different from its natural meaning
DEFINING OPERATOR OVERLOADING
The process of defining an additional task to a predefined operator is called operator overloading. Operator
overloading is done with the help of a special function called operator function. The general form of an operator function is

return-datatype class-name :: operator op(argumentlist)


{
function body
}
Where,
return-datatype – type of value returned by the operator function
operator - keyword
op –operator being overloaded
The operator function must be either member functions or friend functions. A basic difference between them is that
a friend function will have only one argument for unary operators and two arguments for binary operators, while a member
function has no arguments for unary operators and only one argument for binary operators.
Example:
Complex operator + (complex c1, complex c2) //binary operator overloading
{
The process of overloading involves the following steps:
i) Create a class that defines the datatype that is to be used in the overloading operation.
ii) Declare the operator function operator op () in the public part of the class
iii) Define the operator function to implement the required operations.
OVERLOADING UNARY OPERATORS
If the operator used in a operator overloading function is unary operator then this overloading is called unary
operator overloading. The general form is
return-datatype class-name :: operator unary-operator()
{
function body
}

The general form for calling the unary operator function is

Object=unary-operator;
(or)
Object=object unary-operator;
(or)
Object=unary-operator object;
#include<iostream.h>
class minus
{
int a,b,c;
Public:
void input();
void display();
void operator – (); //overload minus operator
};
void minus::input()
{
cout<<“Enter three values”;
cin>>a>>b>>c;
}
void minus::display()
{
cout<<a<<“\t”<<b<<“\t”<<c;
}
void minus::operator – ()
{
a=-a;
b=-b;
c=-c;
}
void main()
{
minus m;
m.input();
cout<<“Before changing sign\n”;
m.display();
-m;
cout<<“After changing sign\n”;
m.display();
}
The above program produces the following output
Enter three values
10
20
30
Before changing sign
10 20 30
After changing sign
-10 -20 -30
OVERLOADING BINARY OPERATORS
If the operator used in a operator overloading function is binary operator then this overloading is called binary
operator overloading. This take one argument from argument list and another one argument rom the calling function.
The general form is
return-datatype class-name :: operator binary-operator(argument)
{
function body
}

The general form for calling the binary operator function is

Object3=object1 binary-operator object2;

Example program:
#include<iostream.h>
class complex
{
float rp,ip;
public:
void input()
{
cout<<”Enter the real part:”;
cin>>rp;
cout<<”Enter the imaginary part”;
cin>>ip;
}
void display()
{
cout<<”Real part is:”<<rp;
cout<<”Imaginary Part is:<<ip;
}
complex operator + (complex c2)
{
complex temp;
temp.rp=rp+c2.rp;
temp.ip=ip+c2.ip
return(temp)
void main()
{
complex c1,c2,c3;
c1.input();
c2.input();
c3=c1+ c2; // binary operator overloading
c3.display();
}
The output is:
Enter the real part:
3.2
Enter the imaginary part
2.1

Enter the real part:


4.3
Enter the imaginary part
1.5
Real part is: 7.5
Imaginary Part is: 3.6
OVERLODING BINARY OPERATORS WITH FRIENDS
friend function can be used in the place of member function for overloading a binary operator. The difference
between operator overloading using member function and using friend is, friend function requires two arguments explicitly
while a member function requires only one.
The general form is,
class classname
{
private:
--------------
public:
friend return-datatype operator symbol(datatype1, datatype2)
};
return-datatype operator symbol(datatype arg1, datatype arg2)
{

statement block
}

Where,
friend -- keyword
operator- keyword
symbol- valid binary operator
The general form for calling the binary operator function using friend is
Object3=object1 symbol object2
When this statement is executed , the symbol calls the overload function and the value of object1 and
object2 are passed to the overload function. Final result will be assigned to object3.
Example program:
#include<iostream.h>
class complex
{
float rp,ip;
public:
void input()
{
cout<<”Enter the real part:”;
cin>>rp;
cout<<”Enter the imaginary part”;
cin>>ip;
}
void display()
{
cout<<”Real part is:”<<rp;
cout<<”Imaginary Part is:<<ip;
}
friend complex operator + (complex c1,complex c2)
};
complex operator + (complex c1,complex c2)
{
complex temp;
temp.rp=c1.rp+c2.rp;
temp.ip=c1.ip+c2.ip
return(temp)
}
void main()
{
complex c1,c2,c3;
c1.input();
c2.input();
c3=c1+ c2; // binary operator overloading
c3.display();
}
The output is:
Enter the real part:
3.2
Enter the imaginary part
2.1
Enter the real part:
4.3
Enter the imaginary part
1.5
Real part is: 7.5
Imaginary Part is: 3.6
MANIPULATION OF STRINGS USING OPERATORS
In c++, there are no operators available for string manipulations, but C++ permits to manipulate strings using
operator overloading.
#include<iostream.h>
#include<string.h>
class string1
{
private:
char str[25];
public:
string1()
{
strcpy(str, “\0”);
}
string1(char str1[25])
{
strcpy(str, str1);
}
void display()
{
cout<<str<<endl;
}
string1 operator +( string1 str1);
};
string1 string1 :: operator + (string1 s2)
{
string1 s3;
int i,j;
int length1=strlen(str);
int length2=strlen(s2.str);
for (i=0;i<length1;i++)
s3.str[i]=s1.str[i];
for (j=length1,i=0;j<length1+length2;j++,i++)
{
s3.str[j]=s2.str[i];
}
S3.str[j]=‘\0’;
return (s3);
}
void main()
{
string1 s1(“arthi”),s2(“ramesh”);
string1 s3;
s3=s1+s2;
cout<<“String1 is:”;
s1.display();
cout<<”String2 is:”;
s2.display();
cout<<”String3 is:”;
s3.display();
getch();
}
The output will be
String1 is : Arthi
String2 is : Ramesh
String3 is : ArthiRamesh
RULES FOR OVERLOADING OPERATORS:
1) Only existing operators can be overloaded. New operators can not be created.
2) The overload operator must have at least one operand
3) We cannot change the basic meaning of an operator .That is , we cannot redefine the plus(+) operator to subtract one
value from the another
4) Overload operator follow the syntax rules of the original operators. They cannot be overridden.
5) There are some operators that cannot be overloaded.
6) We cannot use friend functions to overload certain operators. However, member functions can be used to overload them.
7) Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but,
overloaded by means of a friend function, take one reference argument.
8) Binary operators overloaded through a member function take one explicit arguments and overloaded through a friend
function, take two explicit arguments.
9) When using binary operators overloaded through a member function , left hand operand must be an object of the relevant
class
10) Binary arithimatic operators such as +, -, *, / must explicitly return a value.
TYPE CONVERSION
The conversion of variables or object from one type to another type is called type conversion. Three types of
situation 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 class type to class type
1)Conversion from basic type to class type
Conversion of basic datatype to object of a class is called basic type to class type conversion. Using this
conversion, we can convert basic data types such as int, float etc into class type.
for this conversion constructors are used. This conversion function should be defined in class in the form of
the constructor. The general form is

Constructor(basic type)
{
// statement block
}
Example Program:
#include<iostream.h>
#include<conio.h>
class time
{
int minutes;
int seconds;
public:
time(int s) //constructor function
{
seconds=s/60;
minutes=s*60;
}
};
void main()
{
time t;
int d=85;
t=d;// basic to class type conversion
clrscr();
getch();
}
In the above program, an integer value 85 is converted into object ‘t’.
2) CONVERSION FROM CLASS TYPE TO BASIC TYPE
Conversion of object to basic type is called object type to basic type conversion. (ie) It converts the data
members of an object to basic data types and returns a basic data-item.
For this conversion operator overloading function is used. The general form is
operator basictype()
{
// statement block
}

Where,
operator– keyword
basic type – int , float, char etc

Example:
#include<iostream.h>
#include<conio.h>
class time
{
int minutes;
int seconds;
public:
time(int m,int s) //constructor function
{
minutes=m;
seconds=s;
}
operator int() //operator function
{
int h;
h= seconds+minutes;
return(h);
}
};
void main()
{
time t(85,30);
int d;
d=t;// class to basic type conversion
getch();
}

iii) Conversion from class type to class type


Conversion of one class to another class means assigning the values of object in one class to
object in another class.
This can be implemented in any one of the following method
i)Using constructor function
ii) Using operator function
i) Using constructor function
If constructor function is used, it should be defined inside the destination class. The general
form is
destintion-classname (source-clssname)
{
conversation statements
}
ii) Using operator function
If operator function is used, it should be defined inside the source class. The general form is

operator destination-classname()
{
conversion sttements
}

Example Program:

#include<iostream.h>
#include<conio.h>
class time
{
int seconds;
int minutes;
public:
time(int s,int m) //constructor function
{
seconds=s;
minutes=m;
}
};
class date
{
private:
int hours ;
public:
date(time t1) //constructor function
{
hours=t1.minutes+t1.seconds;

}
void display()
{
cout<<“hours”<<hours;
}
};
void main()
{
time t(50,60);
date d;
d=t; //class to class type conversion
d.display();
getch();
}

You might also like