








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Learn how to overload operators in C++ for both non-member and member functions. Understand the syntax, examples, and benefits of operator overloading for classes like CoinMoney. Discover how to add, multiply, and define unary operators.
Typology: Study Guides, Projects, Research
1 / 14
This page cannot be seen from the preview
Don't miss anything!









class CoinMoney { public: ! CoinMoney(int n = 0, int d=0, int q= 0) : !! nickels(n), dimes(d), quarters(q) {} ! int get_value() const !! {return nickels * 5 + dimes * 10 + quarters * 25;} private: ! int nickels; ! int dimes; ! int quarters; };
CoinMoney operator+ (const CoinMoney& lhs, const CoinMoney& rhs) { ! CoinMoney sum ( !! (lhs.get_nickels() + rhs.get_nickels()), !! (lhs.get_dimes() + rhs.get_dimes()), !! (lhs.get_quarters() + rhs.get_quarters()) !! ) ! return sum; }
! m3 = m1 + m2;
class CoinMoney { ... ! // declare the operator overload as a const member function ! CoinMoney operator+ (const CoinMoney& rhs) const; ... }; CoinMoney CoinMoney::operator+ (const CoinMoney& rhs) const { ! CoinMoney sum( !! (nickels + rhs.nickels), !! (dimes + rhs.dimes), !! (quarters + rhs.quarters) !! ); ! return sum; }
! m3 = m1 + m2; ! m3 = m1.operator+ (m2);
m1.operator* (2.5);
x = my_double_variable.operator* (m1);
x = my_thing * m1;
! m2 = m1 * my_double_variable;
! m2 = my_double_variable * m1;
! m1 < m
! (m1 % m2++) | m
! m1 / 2.
os << x.nickels;
ostream& operator<< (ostream& os, const CoinMoney* p) { ! os << *p; ! return os; }
cout << m1_ptr << endl;
ostream& operator<< (ostream& os, int x) {/* output an integer /} ostream& operator<< (ostream& os, double x) {/ output a double /} ostream& operator<< (ostream& os, char * x) {/ output a C string */}
istream& operator>> (istream& is, CoinMoney& m) { ! /* whatever you want to input and store in m */ ! is >> m.nickels >> m.dimes >> m.quarters; ! return is; }
CoinMoney m1; cin >> m1;! // read member variable values ...
tax = .06 * m1;
operator double () const { ! return get_value(); }
if(my_input_file) {/* everything is good! */}
operator bool (const istream& is) {whatever}
class Thing { blah blah }; class Glob { ! Glob(Thing t); // construct a Glob from a Thing blah blah }; void foo(Glob g); // function foo takes a Glob parameter .... Thing my_thing; ... foo(my_thing); // call foo with a Thing? ...
foo (Glob(my_thing));
return-type operator() (parameters) {function body}
// declare a function object class with function call operator class My_FOC { public: ! bool operator() (int i1, int i2) const ! { !! int sum = i1 + i2; !! return sum % 2; ! } }; int main() { ! My_FOC odd_tester;// create the function object ! int i1, i2; ! cin >> i1 >> i2;! // no error check - for brevity here ! // apply the function object just like a function ! bool result = odd_tester(i1, i2); ! if(result) !! cout << "sum is odd" << endl; ! else !! cout << "sum is even" << endl; ! return 0; }