EECE3326_Jan16_2014 - operator overloading, friend functions

3 Pages

Electrical and Comp Engineering
Course Code
EECE 2560
Waleed Meleis

This preview shows page 1. Sign up to view the full 3 pages of the document.
Introduction to C++ 1/16 Operator Overloading (with global functions) // Sometimes it is easier to read Object1 + Object 2 than: readObject1(class1) readObject2(class2) class1.duration(class2) // class1 + class2 is just more intuitive, and we can build in this functionality into our program with operator overloading // that is, “+” can add two ints, two doubles, etc, but we can overload it to add two classes as well print (….) x = y + z // this is overloaded to refer to an operation on a wide range of data types. Ba returnType operator op (type1 lhs, type2 rhs) _____ t1 == t2 _____ bool operator == (const time24 &1lhs, const time24 &rhs) { return lhs.getHour() == rhs.getHour() && lhs.getMinute() == rhs.getMinute(); } _____ t1 != t2 _____ bool operator != (const time24 &lhs, const time24 &rhs) { return! ( lhs == rhs ) } 1. timeA + timeB 2. timeA + 50 3. 10 + timeA 1. time24 operator + (const time24 &lhs, const time24 &rhs) { int min1 = lhs.getHour()*60 + lhs.getMinute(); int min2 = rhs.getHour()*60 + rhs.getMinute(); return time24(0, min1 + min2); // normalizeTime() is called } 2. time24 operator + (const time24 &lhs, int min1) { int min2 = lhs.getHour()*60 + lhs.getMinute(); return time24(0, min1 + min2); } 3. time24 operator + (int min1, const time24 &rhs) { int min2 = rhs.getHour()*60 + rhs.getMinute(); return time24(0, min1 + min2); } 3a. time24 opera
More Less
Unlock Document

Only page 1 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.