04 Operator Overloading
04 Operator Overloading
OVERLOADING
and
add two objects of class length, len1 and len2, and stores
the result in another object, len3.
Operator Overloading
BASIC PRINCIPLES
Operator Overloading
BASIC PRINCIPLES
#include <iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter() : count(0)
{ }
void inc_count()
{ count = count + 1; }
int get_count()
{ return count; }
};
Operator Overloading
OVERLOADING UNARY OPERATORS
main()
{
counter c1, c2;
Operator Overloading
OVERLOADING UNARY OPERATORS
• Recall that unary operators act on only one operand (an operand is
simply a variable acted on by an operator). Examples of unary
operators are the increment and decrement operators ++ and --,
and the unary minus, as in -33.
Operator Overloading
OVERLOADING UNARY OPERATORS
#include <iostream>
using namespace std;
class counter
{
private:
int count;
The keyword operator is used to
public: overload the ++ operator.
counter() : count(0)
{ } This declarator syntax tells the
void operator ++ () compiler to call this member function
{ count = count + 1; } whenever the ++ operator is
int get_count()
encountered, provided the operand
{ return count; }
(the variable operated on by the ++) is
};
of type counter.
Operator Overloading
OVERLOADING UNARY OPERATORS
main()
The ++ operator is applied to a
{
specific object, as in the
counter c1, c2;
expression ++c1.
cout << "\nc1= " << c1.get_count(); The operator ++() takes no
cout << "\nc2= " << c2.get_count(); arguments.
Operator Overloading
OPERATOR RETURN VALUES
c1 = ++c2;
• This is because the ++ operator was defined to have a return type of void in the
operator++() function, while in the assignment statement it is being asked to
return a variable of type counter. That is, the compiler is being asked to return
whatever value c2 has after being operated on by the ++ operator, and assign
this value to c1.
• Of course the normal ++ operator, applied to basic data types such as int, would
not have this problem.
Operator Overloading
OPERATOR RETURN VALUES
#include <iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter() : count(0)
{ }
int get_count()
{ return count; }
Operator Overloading
OPERATOR RETURN VALUES
Operator Overloading
OPERATOR RETURN VALUES
Operator Overloading
OPERATOR RETURN VALUES
counter operator ++ ()
• Nameless Temporary Objects
{
++count;
counter operator ++ () return (counter (count));
{ }
counter temp;
This statement creates an
++count; object of type counter. This
temp.count = count; object has no name; it will
return temp; not be around long enough
to need one. This unnamed
} object is initialized to the
value provided by the
argument count.
Operator Overloading
OPERATOR RETURN VALUES
#include <iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter() : count(0)
{ }
counter (int c) : count(c)
{ }
int get_count()
{ return count; }
Operator Overloading
POSTFIX NOTATION
#include <iostream>
using namespace std;
class counter
{
private:
int count;
public:
counter() : count(0)
{ }
counter (int c) : count(c)
{ }
int get_count()
{ return count; }
Operator Overloading
POSTFIX NOTATION
Operator Overloading
POSTFIX NOTATION
main()
{
counter c1, c2;
Operator Overloading
OVERLOADING BINARY OPERATORS
#include <iostream>
using namespace std;
class length
{
private:
int feet;
float inches;
public:
length () : feet (0), inches (0.0)
{}
length (int ft, float in) : feet(ft), inches(in)
{}
Operator Overloading
OVERLOADING BINARY OPERATORS
void getlength()
{
cout << "\nEnter Feet: ";
cin >> feet;
cout << "Enter Inches: ";
cin >> inches;
}
void showlength()
{
cout << feet << " feet and " << inches << " inches.";
}
Operator Overloading
OVERLOADING BINARY OPERATORS
Operator Overloading
OVERLOADING BINARY OPERATORS
Operator Overloading
OVERLOADING BINARY OPERATORS
len3.add_dist(len1, len2);
Operator Overloading
OVERLOADING BINARY OPERATORS
#include <iostream>
using namespace std;
class length
{
private:
int feet;
float inches;
public:
length () : feet (0), inches (0.0)
{}
length (int ft, float in) : feet(ft), inches(in)
{}
Operator Overloading
OVERLOADING BINARY OPERATORS
void getlength()
{
cout << "\nEnter Feet: ";
cin >> feet;
cout << "Enter Inches: ";
cin >> inches;
}
void showlength()
{
cout << feet << " feet and " << inches << " inches.";
}
Operator Overloading
OVERLOADING BINARY OPERATORS
Operator Overloading
OVERLOADING BINARY OPERATORS
The output of this program is:
main()
Enter Feet: 10↵
{
Enter Inches: 6.5↵
length len1, len3, len4;
length len2 (11, 6.25);
Length 1 = 10 feet and 6.5 inches.
Length 2 = 11 feet and 6.25
len1.getlength(); inches.
Length 3 = 22 feet and 0.75
len3 = len1 + len2; inches.
len4 = len1 + len2 + len3; Length 4 = 44 feet and 1.5 inches.
Operator Overloading
OVERLOADING BINARY OPERATORS
• In the expression:
• The argument on the left side of the operator (len1 in this case) is
the object of which the operator is a member. The object on the
right side of the operator (len2) must be furnished as an argument
to the operator. The operator returns a value, which can be
assigned or used in other ways; in this case it is assigned to len3.
Operator Overloading
COMPARISON OVERLOADING
#include <iostream>
using namespace std;
class length
{
private:
int feet;
float inches;
public:
length () : feet (0), inches (0.0)
{}
length (int ft, float in) : feet(ft), inches(in)
{}
Operator Overloading
COMPARISON OVERLOADING
void getlength()
{
cout << "\nEnter Feet: ";
cin >> feet;
cout << "Enter Inches: ";
cin >> inches;
}
void showlength()
{
cout << feet << " feet and " << inches << " inches.";
}
Operator Overloading
COMPARISON OVERLOADING
Operator Overloading
COMPARISON OVERLOADING
The output of this program is:
main() Enter Feet: 5↵
{ Enter Inches: 11.5↵
length len1;
length len2 (6, 2.5); Length 1 = 5 feet and 11.5 inches.
Length 2 = 6 feet and 2.5 inches.
Operator Overloading
DATA CONVERSION
intvar1 = intvar2;
len1 = len2;
Operator Overloading
DATA CONVERSION
• In a statement like:
intvar = floatvar;
Operator Overloading
DATA CONVERSION
Operator Overloading
DATA CONVERSION
#include <iostream>
using namespace std;
class length
{
private:
int feet;
float inches;
public:
length () : feet (0), inches (0.0)
{}
length (int ft, float in) : feet(ft), inches(in)
{}
Operator Overloading
DATA CONVERSION
void getlength()
{
cout << "\nEnter Feet: ";
cin >> feet;
cout << "Enter Inches: ";
cin >> inches;
}
void showlength()
{
cout << feet << " feet and " << inches << " inches.";
}
Operator Overloading
DATA CONVERSION
length len1(2.35);
Operator Overloading
DATA CONVERSION
Operator Overloading
DATA CONVERSION
mtrs = len2;
cout << “\nLength 2 = “ << mtrs << “ meters\n”;
}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
#include <iostream>
using namespace std;
class time12
{
private:
int pm, hrs, mins;
public:
time12() : pm(1), hrs(0), mins(0)
{}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
void display()
{
cout << hrs << ":";
if (mins < 10)
cout << 0;
cout << mins << " ";
if (pm == 1)
cout << "p.m.";
else
cout << "a.m.";
}
};
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
class time24
{
private:
int hours, minutes, seconds;
public:
time24() : hours(0), minutes(0), seconds(0)
{}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
if (mins12 == 60)
{
mins12 = 0;
hrs12 = hours + 1;
}
else
hrs12 = hours;
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
main()
{
int h, m, s; time12 t12;
The output of this program is:
cout << "Enter 24-hour Time\n";
cout << " Hours: "; Enter 24-hour Time
cin >> h; Hours: 23
cout << " Minutes: "; Minutes: 59
cin >> m; Seconds: 45
cout << " Seconds: "; You entered: 23:59:45
cin >> s; 12-hour Time: 12:00 a.m.
time24 t24(h, m, s);
cout << "You entered: ";
t24.display();
t12 = t24;
cout << "\n12-hour time: ";
t12.display();
}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
#include <iostream>
using namespace std;
class time24
{
private:
int hours, minutes, seconds;
public:
time24() : hours(0), minutes(0), seconds(0)
{}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
class time12
{
private:
int pm, hrs, mins;
public:
time12() : pm(1), hrs(0), mins(0)
{}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
if (pm == 1)
cout << "p.m.";
else
cout << "a.m.";
}
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
Operator Overloading
CONVERSION OF OBJECTS OF DIFFERENT CLASSES
main()
{
int h, m, s;
t12 = t24;
cout << "\n12-hour time: ";
t12.display();
}
Operator Overloading