Week 11 Lec 01 - Operator Overloading
Week 11 Lec 01 - Operator Overloading
Oriented Programming
Lecture # 16
Wednesday, April 14, 2022
Spring 2022
FAST – NUCES, Faisalabad Campus
Muhammad Yousaf
2
Operator Overloading
} return 0;
}; }
CS1004 - Objected Oriented Programming
5 Overloading Unary Operator pre-increment (++)
The code in the last slide does not allow the int main()
following statement to execute {
c1 = ++c2; Counter c1, c2;
cout << "\nc1 = " <<
Because the return type is void
c1.get_count(); cout << "\nc2 =
To overcome this problem " << c2.get_count();
Counter operator ++ ()
++c1; //increment c1
{
++count; //increment count c2 = ++c1;
Counter temp; //make a temporary
//Counter
cout << "\nc1 = " <<
c1.get_count();
temp.count = count; //give it same cout << "\nc2 = " <<
//value as this c2.get_count()
obj << endl;
return temp; //return the copy return 0;
}
}
class dist1.getdist();
Distance dist2(11, 6.25);
if (dist1 < dist2)
bool Distance::operator < cout << "\ndist1 is less than"
<< "dist2";
(Distance dt) const
else
{ cout << "\ndist1 is greater than"
float d1 = feet + inches / << " (or equal to) dist2";
12; cout << endl;
float d2 = dt.feet + cout << "dist1 = ";
dt.inches / 12; dist1.showdist();
return (d1 < d2)?true : cout << endl;
cout << "dist2 = ";
false;
dist2.showdist();
} cout << endl;
}