0% found this document useful (0 votes)
16 views16 pages

Lab 6

Uploaded by

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

Lab 6

Uploaded by

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

Lab 6

Operator Overloading

1
Table of Contents

• Operators in C++
• Why we need Operator Overloading?
• Operator Overloading Definition
• Defining Operator Overloading in C++
• Overloading Unary Operators
• Postfix and Prefix Notation
• Overloading Binary Operators
• TRY IT YOURSELF

2
❑ Operators in c++
❑ Why we need Operator Overloading?

Add two int numbers Add two strings Add two Objects

int main() { int main() { int main() {

int num1=3; string s1=“OOP”; Distance d1;

int num2=4; string s2=“ Course”; Distance d2;

int res= num1+num2; string res= s1+s2; Distance d3=d1+d3;

cout<<res; cout<<res; cout<<d3;

return 0; return 0; return 0;

} } }

Output: Output: Output:


7 OOP Course
error

main.cpp:20:1: error: no match for ‘operator+’


(operand type is ‘Counter’)
4
❑ Operator Overloading Definition

operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings
when they are applied to user-defined data types.
Normally
a = b + c;
works only with basic types such as int and float, and attempting to apply it when a, b, and c
are objects of a user-defined class will cause complaints from the compiler. However, using
overloading, you can make this statement legal even when a, b, and c are user-defined types.
❑ Defining Operator Overloading in C++

Is the type of the value returned by the specific operation

return-type Class-name :: Operator OP (argList)


{
function body
The operator being overloaded
} +, – , ++, and so on.
❑ Overloading Unary Operators

class Counter{
private:
unsigned int count; //count
public: int main()
Counter() : count(0){} //constructor {
Counter(int val): count(val){} //constructor Counter c1, c2; //c1=0, c2=0
cout << "\nc1="<< c1.get_count(); // 0
unsigned int get_count(){ //return count cout << "\nc2="<< c2.get_count(); // 0
return count; ++c1; //c1=1
} c2 = ++c1; //c1=2, c2=2
Counter operator ++ () { //increment count cout << "\n="<< c1.get_count(); // 2
count++; //increment count cout << "\n="<< c2.get_count() << endl; // 2
Counter temp; //make a temporary Counter return 0;
temp.count = count; //give it same value as this obj }
return temp; //return the copy
}
};

The operator ++ which you


want to be overloaded
7
❑ Postfix and Prefix Notation

int main()
class Counter { {
private: Counter c1, c2;
unsigned int count; //count cout << "\nc1="<< c1.get_count();
public: cout << "\nc1="<< c2.get_count();
Counter() : count(0) //constructor no args ++c1;
{ } c2 = ++c1;
Counter(int c) : count(c) //constructor, one arg cout << "\nc1="<< c1.get_count(); //display
{ } cout << "\nc2="<< c2.get_count();
unsigned int get_count() const //return count c2 = c1++;
{ return count; } cout << "\nc1="<< c1.get_count(); //display again
Counter operator ++ () //increment count (prefix) cout << "\nc2="<< c2.get_count() << endl;
{ //increment count, then return return 0;
return Counter(++count); //an unnamed temporary object }
} //initialized to this count What is the output of this program?
Counter operator ++ (int) //increment count (postfix) c1=0
{ //return an unnamed temporary
c2=0
return Counter(count++); //object initialized to this
} //count, then increment count c1=2
}; c2=2
c1=3
c2=2
8
❑ Overloading Binary Operators Add two objects
#include<iostream>
using namespace std;
class Distance{ //English Distance class
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0) { } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in) { }
void getdist(){ //get length from user
cout << "\nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
void showdist() const { //display distance
cout << feet << "\'-" << inches << '\"';
}
Distance operator + ( Distance ) const; //add 2 distances
};

9
❑ Overloading Binary Operators Add two objects

Distance Distance::operator + (Distance d2) const //return sum


{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
while(i >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
i -= 12.0; //by 12.0 and
f++; //increase feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to sum
}

10
❑ Overloading Binary Operators Add two objects

int main()
{
Distance dist1, dist3, dist4; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single ‘+’ operator
dist4 = dist1 + dist2 + dist3; //multiple ‘+’ operators
// display all lengths
cout << "dist1 = ";
dist1.showdist();
cout << endl;
cout << "dist2 = ";
dist2.showdist();
cout << endl;
cout << "dist3 = ";
dist3.showdist();
cout << endl;
cout << "dist4 = ";
dist4.showdist();
cout << endl;
return 0;
}

11
TRY IT YOURSELF

Create class called Distance and do the following:


• Properties are private and methods are public
• Properties
• feet: int
• inches: float
• Methods
• Constructor(no arguments) // initialization by 0
• Constructor(feet, inches) // constructor gets cat info from main function
• getdist() \\get the distance information
• Showdist() \\display the distance information
• bool Distance:: operator <(Distance d2) \\compare this distance with d2 distance

12
TRY IT YOURSELF
solution
#include<iostream>
using namespace std;
class Distance { //English Distance class
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) { } //constructor (no args)
Distance(int ft, float in) : feet(ft), inches(in) { } //constructor (two args)
void getdist(){ //get length from user
cout << "\nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
void showdist() const { //display distance
cout << feet << "\'-" << inches << '\"';
}
bool operator < ( Distance ) const;
};
13
TRY IT YOURSELF
solution

bool Distance::operator < (Distance d2) const


{

float bf1 = feet + inches/12;


float bf2 = d2.feet + d2.inches/12;
return (bf1 < bf2) ? true : false;
}

14
TRY IT YOURSELF
solution int main()
{
Distance dist1; //define Distance dist1
dist1.getdist(); //get dist1 from user
Distance dist2(6, 2.5); //define and initialize dist2
//display distances
cout << "\ndist1 = "; dist1.showdist();
cout << "\ndist2 = "; dist2.showdist();
if( dist1 < dist2 ) //overloaded ‘< operator
cout<< "\ndist1 is less than dist2";
else
cout << "\ndist1 is greater than (or equal to) dist2";
cout << endl;
return 0;
}

15
THANK YOU

16

You might also like