Operator Overloading L7
Operator Overloading L7
OVERLOADING
Satyendra Singh Chouhan
Assistant professor (CSE)
8/21/2017 2
Operator overloading
• With objects, an operator, like “+”, is really just a function
with a different syntax
• With a binary operator, the arguments are placed on
either side of the operator
string air = "air", plane = "plane";
string combined = air + plane;
lhs rhs
8/21/2017 3
Operator overloading
• Just like any function, operators can be overloaded
Operator overloading
• We have to overload operators for the classes that we create
Operator overloading
Restrictions
• Precedence of operators cannot be changed
S: = 10 -20 30
-S: = -10 20 -30
Overloading unary operator using friend
function
#include<iostream>
using namespace std;
class space
{
int x;
int y;
int z;
public:
void set_data(int a, int b,int c);
void display(void);
friend void operator-(space &s);//overloading unary
minus
};
Defining all member functions
void space::set_data(int a,int b,int c)
{
x=a; y=b; z=c;
}
void space::display(void)
{cout<<x<<“ ”<<y<<“ ”<<z<<“\n”;}
class Book{
public:
Book( string t="",int cpy=0 ): title(t), copies(cpy){}
void setTitle( string t ){ title = t; }
string getTitle(){ return title; }
void setCopies ( int cpy ){ copies = cpy; }
int getCopies(){ return copies; }
string toString(){ return "Book: " + title; }
private:
string title;
int copies;
};
#endif
24
Binary Operators
• Operands are placed on both sides of the operators
• Example: the equality operator
• Usage
Book book1("C++ How to Program",1);
Book book2("Java How to Program",1);
if( book1 == book3 ) cout << "Equal" << endl;
Unary Operators
• Only one operand
• Example: the not operator
• Usage
Book book3;
if( !book3 ) cout << "empty" << endl;
lhs
• Implementation
class Book{
public:
bool operator!(){
return ( title=="" && copies==0 );
}
The conditions of an “empty” object
26
• Implementation
class Book{
friend ostream& operator<<( ostream& out, const Book& rhs );
//remainder of the Book class here
};
lhs rhs
Global function definition:
ostream& operator<<( ostream& out, const Book& rhs ){
out << rhs.title;
A friend has access to the
return out; private data members
}
28
• Implementation
class Book{
friend istream& operator>>( istream& in, Book& rhs );
//remainder of the Book class here
};
Global function definition:
istream& operator>>( istream& in, Book& rhs ){
in >> rhs.title;//captures a single word, no whitespace
in >> rhs.copies;
return in;
}
29
Overloading ++
• Special syntax to distinguish pre- from post-increment
• Example:
• Usage
book1++;
cout << "Copies = " << book1.getCopies() << endl;
int mCount;
Space()
{
mCount = 10;
} int main()
{
Space operator ++() Space objSpace;
{ ++ objSpace;
mCount++; cout<<++objSpace.mCount;
return Space(mCount); return 0;
} }
};
Type Conversions
int m ;
float x=3.14159;
m = x; //compiler converts x to an integer before its
assignment
Consider this: obj=obj1+obj2;// where obj,obj1and obj2 are
object of same class
• The assignments between basic types or user defined
types are taken care by the compiler provided the data
type on both sides of = are of same type.
• But what to do in case the variables are of different types
on both sides of the = operator? In this case we need to
tell to the compiler for the solution.
• Ex.obj=20;
8/21/2017 35
• Constructor converts duration from int to class type and then assigns
the time type to the values of the object time
• After conversion, the hrs member of t2 will contain a value of 1 and
mins member a value of 25.
8/21/2017 38
Exercise
Write c++ program which has class distance containing
data members feet and inches . also write the necessary
type conversion code to allow following statement in main
function.
distance dist1; //object dist1 created
int length = 20;
dist1=length; //int to class type
Sample output:
feet=1 and inches=8
Class to Basic Type
• Constructor cant do this type of conversion.
• C++ allow us to define overloaded casting(conversion )
operator that is used to convert a class type data to a
basic data type.
• General form:
operator typename()
{
function statements
}
This function converts a class type data to typename.
Eg. operator int() converts a class object to type int
operator double() converts a class object to type
double
Eg for overloaded casting operator
vector :: operator double()
{
double sum=0;
for(int i=0; i<size; i++)
sum=sum + v[i] * v[i];
return sqrt(sum);
}
• The function converts a vector to the corresponding
scalar magnitude.
• The magnitude of a vector is given by square root of
the sum of the squares of its components.
double length=double(v1); or
double length=v1; // When a class type to a basic type conversion is
required, the compiler will call the casting operator function for
performing this task
Casting operator function
• 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.
One class to another class
For example,
studdata obj1;
result obj2;
• obj2 = obj1; //different type of objects
• We are converting the class studdata data type to class result type
data and the value is assigned to obj2.
• Here studdata is known as source class and result is known as the
destination class.
• The above conversion can be performed in two ways :
(a) Using a constructor.
(b) Using a conversion function.
One class to another class
• If we take a single-argument constructor function for
converting the argument’s type to the class type (whose
member it is). So the argument is of the source class and
being passed to the destination class for the purpose of
conversion. Therefore it is compulsory that the conversion
constructor be kept in the destination class.
• Example:
• Constructor function to be placed in destination class.
class X
{
public:
X(Y obj_y)
{
}
One class to another class(cont…)
• When we need to convert a class, a casting operator
function can be used i.e. source class. The source class
performs the conversion and result is given to the object of
destination class.
Example:
class Y
{
public:
operator X();
};
Y::operator X()
{
// definition
}
8/21/2017 45
class Miles{
private: double miles;
public:
Miles(double miles) : miles(miles) {}
void display() { cout << miles << " miles";
}
operator Kilometers() {
return Kilometers(miles*1.609344); }
Miles(Kilometers kilometers)
{
miles = kilometers.getValue()/1.609344;
}
};
8/21/2017 47
int main(void){
/* * Converting using the conversion function */
Miles m1 = 100;
Kilometers k1 = m1;
m1.display(); cout << " = ";
k1.display(); cout << endl; /* Converting using the constructor */
Kilometers k2 = 100;
Miles m2 = k2; // same as: Miles m2 = Miles(k2);
k2.display();
cout << " = ";
m2.display();
cout << endl;
}
8/21/2017 48
O/P
• 100 miles = 160.934 kilometeres
• 100 kilometeres = 62.1371 miles
8/21/2017 49
Problem:Overload ++
• How to differentiate overloading of pre-increment and post-increment
operators?
• To make both versions of the increment operator work, we define two
overloaded ++ operators,
• For overloading pre-increment ++, usual way of unary operator will be
followed.
• Eg. Returnt_type operator++()
• For overloading post-increment C++ developer used concept of dummy
parameter
• Eg. Return_type operator++(int)
• Note: The only difference is the int in the parentheses. This int isn’t
really an argument, and it doesn’t mean integer. It’s simply a signal to
the compiler to create the postfix version of the operator
8/21/2017 51
Cont…
// overloaded ++ operator in both prefix and postfix
#include <iostream>
using namespace std;
class Counter
{ private:
int count; //count
public:
Counter() : count(0) //constructor no args
{}
Counter(int c) : count(c) //constructor, one arg
{}
8/21/2017 52
Cont…
int get_count() const //return count
{
return count;
}
Counter operator ++ () //increment count (prefix)
{
return Counter(++count); //an unnamed temporary object
}
Counter operator ++ (int) //increment count (postfix)
{
return Counter(count++); / /return an unnamed temporary object initialized to
} this count, then increment count
};
8/21/2017 53
Cont…
int main()
{
Counter c1, c2; //c1=0, c2=0
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count();
++c1; //c1=1
c2 = ++c1; //c1=2, c2=2 (prefix)
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count();
c2 = c1++; //c1=3, c2=2 (postfix)
cout << “\nc1=” << c1.get_count(); //display again
cout << “\nc2=” << c2.get_count() << endl;
return 0;
}
8/21/2017 54
Output:
c1=0
c2=0
c1=2
c2=2
c1=3
c2=2
8/21/2017 55
Exercise 1
Create a class RationalNumber (fractions) with the following
capabilities:
a.) Create a constructor that prevents a 0 denominator in a fraction,
reduces or simplifies fractions that are not in reduced form and avoids
negative denominators.
b.) Overload the addition, subtraction, multiplication and division
operators for this class.
c.) Overload the relational and equality operators for this class.
• Sample output 1
Enter a Rational Number (n/d): 1/3
Enter a Rational Number (n/d): 2/4
1/3 + 1/2 = 5/6
1/3 - 1/2 = -1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
1/3 <= 1/2 according to the overloaded > operator
1/3 < 1/2 according to the overloaded >= operator
1/3 < 1/2 according to the overloaded < operator
1/3 <= 1/2 according to the overloaded <= operator
1/3 != 1/2 according to the overloaded == operator
1/3 != 1/2 according to the overloaded != operator
8/21/2017 57
Exercise 2
• Develop class Polynomial. This internal representation of
a polynomial is an array of terms. Each term contains a
coefficient and an exponent, hence you can have an array
for coefficients and exponents as a member of your
class(maximum number of terms will 100 however your
class should also contain a member variable that
indicates how many terms are there in a given instance of
the polynomial class).
Eg. the term 2x^4 has a coefficient of 2 and exponent of 4.
• Develop a full class containing proper constructor and
destructor functions as well as set and get functions.
8/21/2017 58
Exercise
• The class should also provide the following overloaded
operator capabilities:
1 overload the addition operator (+) to add two
polynomials.
2 overload the subtraction operator (-)to subtract two
polynomials
3 overload the assignment operator to assign one
polynomial to another
4 overload the multiplication operator(*) to multiply two
polynomials