









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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Operator overloading in C++ and demonstrates how to implement the Money class with the +, ==, <, and << operators. The code includes the header file, implementation file, and an example application program. Overloading operators as member functions is advocated for better encapsulation and easier implementation.
What you will learn
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
o operator+ o operator++ o operator<< o operator==
returnType operator OperatorSymbol ( parameterList );
int main() { int x, y = 3; x = y + 10; // OK (int + int), other arithmetic operators - , *, /, % // also OK (int = int) -- assignment if (x < y) // OK (int < int), other logical operators <=, >, >=, ==, != ...
class Money { public: Money(); Money(int d, int c); Money(int allc); double getAmount(); // Returns the amount as a double void printMoney(); // prints a money to cout, in the form $xx.yy private: int dollar; int cent; }; int main() { Money m1(3, 25), m2(19, 5);
#ifndef MONEY_H #define MONEY_H #include
#include "money.h" Money::Money(int allc) { dollar = allc / 100; cent = allc % 100; } Money Money::operator+(const Money & m2) const
int total = (dollar * 100 + cent) + (m2.dollar * 100 + m2.cent); Money local(total); return local; } Money Money::operator-(const Money & m2) const // this - mo { int diff = (dollar * 100 + cent) - (m2.dollar * 100 + m2.cent); Money local(diff); return local; } Money Money::operator-() const // unary - { int neg = - (dollar * 100 + cent); Money local(neg); return local; } bool Money::operator==(const Money & m2) const { int thistotal = (dollar * 100 + cent); int m2total = (m2.dollar * 100 + m2.cent); return (thistotal == m2total); } bool Money::operator<=(const Money & m2) const { int thistotal = (dollar * 100 + cent); int m2total = (m2.dollar * 100 + m2.cent); return (thistotal <= m2total); / if (*this < m2 || *this == m2) return true; else return false; */ } // no keyword "friend" in the function definition ostream& operator<<(ostream& out, const Money & m) { out << "$" << m.dollar // dollar private in m -- OK << "." << m.cent; // cent private in m -- OK return out; } istream& operator>>(istream& in, Money & m) { char dollarSign; double moneyAsDouble;
if (m1 > m2) cout << "Greaterthan.\n"; else cout << "NOT Greaterthan.\n"; bool ans = m1 > m2; cout << ans << endl; // prints 0 (false) or 1 (true) system("pause"); return 0; } Run time ouput $2.98 + $15.2 = $18. Not equals. NOT Greaterthan. 0
class Money { public: Money(); Money(int d, int c); Money(int allc); int getDollars() const; int getCents() const; ... // note: NO method for operator< private: int dollar; int cent; }; // Definition of regular, non-member functions. // mo1 < mo bool operator<(const Money & m1, const Money & m2) // note: 2 arguments and NO Money::
int thistotal = m1.getDollars() * 100 + m1.getCents(); int m2total = m2.getDollars() * 100 + m2.getCents(); return (thistotal < m2total); }
class Money { public: Money(); Money(int d, int c); Money(int allc); Money operator+(const Money & mo2) const; ... // friend functions friend ostream& operator<<(ostream& out, const Money & m); // to be able to do cout << obj friend istream& operator>>(istream& in, Money & m); // to be able to do cint >> obj private: int dollar; int cent; }; // no keyword "friend" in the function definition ostream& operator<<(ostream& out, const Money & m) { out << "$" << m.dollar // dollar private in m -- OK << "." << m.cent ; // cent private in m -- OK return out; } istream& operator>>(istream& out, Money & m) { char dollarsign; double moneyAsDouble; in >> dollarSign; // first eat up '$' in >> moneyAsDouble; // xx.yy m.dollar = static_cast
Money operator*(double r, const Money & m) { int total = m.toAllCents() * r; Money local(total); return local; }
int main() { Money m1(2, 98); cout << m1; }
int main() { Money m1(3, 25), m2; m2 = m1 + 6; // 2nd operand is int, not Money. // This int is promoted to a Money object by // the Money constructor which has one int argument // if it is defined (and in our example, it is).
// A class with an array of five double's. class DoubleArray
public: DoubleArray5(double initvalue); ... double & operator[](int index) const; // index is the parameter private: int ar[5]; }; // NOTE: return by reference(&) double & DoubleArray5operator[](int index) const { if (index <= 0 || index > 5) { cout << "ILLEGAL INDEX.\n"; exit(1); } else return ar[index]; } int main() { DoubleArray5 myarray(0.0); double d = myarray[2]; // myarray.operator myarray[1] = 5.7; // can be used on the LHS of = ... }
// A counter class class Counter { public: Counter(int c = 0); // initializes 'count' to c ... Counter operator++(); // pre-increment Counter operator++(int i); // post-increment private: int count; }; Counter Counter::operator++() { // First, increment 'count'. count++; // Second, create a local object with the new count. Counter local(count); // Third, return the local object. return local; }