0% found this document useful (0 votes)
138 views6 pages

CHAPTER 7: Operator Overloading and Type Conversion: by Darshit Shah Page 1 of 6

The document discusses operator overloading in C++. It explains that operator overloading allows operators like + and - to be used with user-defined types like classes, by defining special operator functions. It provides the general form for overloading operators as member or friend functions. The document also discusses using operator overloading to define operations for a complex number class, and different types of type conversions between built-in and user-defined types using constructors and casting operators.

Uploaded by

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

CHAPTER 7: Operator Overloading and Type Conversion: by Darshit Shah Page 1 of 6

The document discusses operator overloading in C++. It explains that operator overloading allows operators like + and - to be used with user-defined types like classes, by defining special operator functions. It provides the general form for overloading operators as member or friend functions. The document also discusses using operator overloading to define operations for a complex number class, and different types of type conversions between built-in and user-defined types using constructors and casting operators.

Uploaded by

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

C++

CHAPTER 7: Operator Overloading and Type Conversion

Introduction:
We use different operators like +, -, * on basic data types like int, float or double. Can we use them
for user defined data types like arrays, structures or classes? E.g.
In order to join two strings, we write following code.
char name1[30] = “Nirma ” ;
char name2[30] = “University”;
char fullname[60];
strcpy (fullname,name1);
strcat (fullname, name2);
Instead of the above program, can we write,
fullname = name1 + name2;

The Answer: In C, No. In C++, Yes.


It is known as operator overloading or compile time polymorphism. Operator overloading teaches a
normal operator to act on a user-defined operand (arrays, structures, classes). It gives additional
meaning to operators like +, *, - , ++ , -- . User-defined data types behave in much the same way as
the standard data types.
We must have to specify what an additional task to an operator means in relation to the class to
which the operator is applied.
It is done with the help of a special function known as operator function that describes the task.

The General Form of Operator Overloading:


Return-type classname:: operator op(arglist)
{
function body; // task defined
}
Here,
 Return-type is the type of value returned by the specified operator,
 op is the operator being overloaded.
 op is preceded by keyword operator. operator is the keyword in C++.
 operator op is the function name.

Characteristics of Operator Overloading:


 It has to be defined in public section only.
 Only existing operators can be overloaded.
 The overloaded operator must have at least one user-defined operand.
 The rules of precedence do not change with operator overloading.
 Overloading of operator function is allowed.

Look at the following program listing. It contains various operators being overloaded for a class
complex.

#include <iostream.h>
#include <conio.h>

class complex
{
float r, i;
public:

By Darshit Shah Page 1 of 6


C++
// function having object as an argument & returning object.
complex add(complex c)
{
complex t;
t.r = r + c.r;
t.i = i + c.i;
return t;
}
complex operator + ( complex c); // Overloaded + Operator

// Adding two complex objects with the help of friend function.


friend complex add( complex , complex);

/* Overloading Operator + using friend function.


In order to work the foll. function properly, you need to comment
operator + function & remove comment in next line.*/
// friend complex operator + (complex, complex);

complex operator * ( complex c) // Overloaded * operator


{
complex t;
t.r = r * c.r - i * c.i;
t.i = r * c.i + i * c.r;
return t;
}
void operator++(int) // Overloading Postfix Unary ++ Operator.
{
r++; i++;
}
void operator--() // Overloading prefix Unary -- Operator.
{
--r; --i;
}
void operator -() // Overloading Unary - Operator.
{
r = -r; i = -i;
}
// Overloading >> and << operator.
friend istream & operator >> (istream & stream, complex & c);
friend ostream & operator << (ostream & stream, complex & c);

};
complex add ( complex c1 , complex c2)
//complex operator + ( complex c1 , complex c2)
{
complex c3;
c3.r = c1.r + c2.r;
c3.i = c1.i + c2.i;
return c3;
}
complex complex::operator + ( complex c)
{
complex t;
t.r = r + c.r;
t.i = i + c.i;
return t;
}
istream & operator >> (istream & stream, complex & c)
{
cout << "Enter values of Real & Imaginary :";
stream >> c.r >> c.i;
return stream;
}

By Darshit Shah Page 2 of 6


C++
ostream & operator << (ostream & stream, complex & c)
{
stream << c.r << " + i " << c.i << endl;
return stream;
}

ostream & tab (ostream & stream) // Creating your own manipulator.
{
stream << "\t";
return stream;
}

void main()
{
complex c1,c2,c3,c4,c5;
cin >> c1 >> c2;
c3 = c1.add(c2);
c4 = c1 + c2; // Internally converted as c4 = c1.operator +(c2);
c5 = add(c1,c2);
cout << "c1 = " << c1 << "c2 = " << c2;
cout << "c3 = " << c3 << "c4 = " << c4 << "c5 = " << c5;
c4 = c1 + c2 * c3; // c4 = c1.operator+ (c2.operator *(c3));
cout << "c4 = " << c4;
--c1;
c1--; // This line generates warning.
c2++;
-c3;
cout << "c1 = " << c1 << "c2 = " << c2 << "c3 = " << c3;
cout << tab << "c1 = " << c1 << tab << "c1+c2 " << c1+c2 << endl;
}

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 member function will have no argument for unary operators and one
argument for binary operators.

The following operators can not be overloaded.


Operators Operators using friend function
sizeof = Assignment Operator
. Membership Operator ( ) Function call Operator
.* Pointer-to Member Operator [] Subscripting Operator
:: Scope Resolution Operator -> Class member access operator
?: Conditional Operator
Exercise:
1. Define friend function for unary – in the above program. What other changes are required to
be done in the program?
2. Define unary – function in such a way that c2 = -c1; would be valid statement.
3. Define operator + function that adds a scalar value to an object. Try writing following
statements in main( ).
c3 = c1 + 2;
c4 = 3 + c2;
4. Define operator = function that should assign one complex object’s value to another
complex object.
5. Define operator = = function that should return true if both objects contain same data
otherwise false.

By Darshit Shah Page 3 of 6


C++

Type Conversion:

In this section, we will see following three types of Conversion:


1. Conversion from in-built data-type to user-defined data-type.
2. Conversion from user-defined data-type to in-built data-type.
3. Conversion from one user-defined data-type to another user-defined data-type.

Conversion from in-built data-type to user-defined data-type:

This type of conversion is achieved using constructor.


Look at the following example.

#include <iostream.h>
#include <conio.h>

class weight
{
int kg, gram;
public:
// Constructor function used to convert basic data-type to user-defined
// data-type.
weight(int g)
{
kg = g / 1000;
gram = g % 1000;
}
// Function to convert Class data-type to basic data-type.
operator int()
{
return (kg * 1000 + gram);
}
void display(void)
{
cout << kg << " Kilogram & " << gram << " Gram " << endl;
}
};

void main()
{
int g = 3500, gram;
clrscr();
weight w1(g); // Conversion from built-in to user-defined data type.
w1.display();
weight w2 = g; // Conversion from built-in to user-defined data type.
w2.display();
gram = w2; // Conversion from user-defined to built-in data type.
cout << "Gram = " << gram;
getch();
}

In the above example, weight w1(g); and weight w2 = g; these two lines calls the constructor
function of class weight. The constructor function converts grams into kilogram and grams and
assigns the values to variables kg & gram respectively.

Conversion from user-defined data-type to in-built data-type:

This type of conversion is achieved using overloaded type-casting operator function.

By Darshit Shah Page 4 of 6


C++

The General form of an overloaded casting operator (conversion function) is as under:


operator typename( )
{
… function statement(s); …
}
Again, refer to the above mentioned example. The conversion is achieved with the help of
operator int( ) function.

The casting operator function should satisfy the following conditions:


 It must be a class member.
 It must not specify a return-type.
 It must not have any arguments.

Conversion from one user-defined data-type to another user-defined data-type:

This can be achieved by two different methods.


1. Using Casting Operator function is source class.
2. Using constructor function in destination class

Using Casting Operator function is source class.


Look at the following code.

#include <iostream.h>
#include <conio.h>
class weightgram
{
int gram;
public:
weightgram(int g)
{
gram = g;
}
weightgram(float kg)
{
gram = kg * 1000;
}
void display(void)
{
cout << gram << " Grams" << endl;
}
};
class weightkg
{
float kg;
public:
weightkg(float kg)
{
weightkg::kg = kg;
};
operator weightgram()
{
weightgram g(kg);
return g;
}
void display(void)
{
cout << kg << " Kilogram" << endl;
}
};

By Darshit Shah Page 5 of 6


C++
void main()
{
weightkg k(4.5);
weightgram g = k;
clrscr();
k.display();
g.display();
getch();
}

In the above program, k is the object of source class while g is the destination
class object. The conversion is achieved by specifying casting operator function
in the source class weightkg.

Using Constructor function in destination class:


#include <iostream.h>
#include <conio.h>
class weightgram;
class weightkg
{
float kg;
public:
weightkg(float kg)
{
weightkg::kg = kg;
}
float givedata(void)
{
return kg;
}
void display(void)
{
cout << kg << " Kilogram" << endl;
}
};
class weightgram
{
int gram;
public:
weightgram(weightkg k)
{
gram = k.givedata() * 1000;
}
void display(void)
{
cout << gram << " Grams" << endl;
}
};
void main()
{
weightkg k(4.5);
weightgram g = k;
clrscr();
k.display();
g.display();
getch();
}
Here, source class object is passed as argument to the constructor function in destination class that
extracts information from source class and passes on it to the destination class. 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 through functions like givedata( ).

By Darshit Shah Page 6 of 6

You might also like