Course Materials

OOP - Week 8: Programming Questions

Note: Try to solve these questions on your own before checking the solutions. If you are stuck, reread Lecture 1, Lecture 2 and Lecture 3. In Question 2, write the whole program in one file.

Filter:
Sort:

Question 1

Trace the following program. Every operator of Money prints a short message when it runs.

#include <iostream>
using namespace std;

class Money {
private:
    int cents;

public:
    Money(int amount = 0) : cents(amount) {
    }
    Money operator+(const Money& other) const {
        cout << "+" << endl;
        return Money(cents + other.cents);
    }
    Money& operator+=(const Money& other) {
        cout << "+=" << endl;
        cents += other.cents;
        return *this;
    }
    bool operator==(const Money& other) const {
        cout << "==" << endl;
        return cents == other.cents;
    }
    Money& operator++() {
        cout << "prefix" << endl;
        cents++;
        return *this;
    }
    Money operator++(int) {
        cout << "postfix" << endl;
        Money old = *this;
        cents++;
        return old;
    }
    friend ostream& operator<<(ostream& out, const Money& m);
};

ostream& operator<<(ostream& out, const Money& m) {
    out << m.cents;
    return out;
}

int main() {
    Money a(5);
    Money b(7);
    Money c = a + b + b;
    cout << c << endl;
    a += b;
    cout << a << endl;
    cout << ++a << endl;
    cout << b++ << endl;
    cout << b << endl;
    if (a == c)
        cout << "same" << endl;
    return 0;
}

Write the exact output of the program. Do not run it before you have written your answer.

Solution
+
+
19
+=
12
prefix
13
postfix
7
8
==

                    

Question 2

Write the class Money, an amount of money in cents, and a main that uses it. Everything goes in one file.

Money has:

  • A private data member cents of type int, and a constructor with a default parameter of 0.
  • A member operator + that returns the sum of two amounts as a new Money.
  • A member operator *= that multiplies the amount by an int and changes the object. It must work in a chain: (a *= 2) *= 3.
  • A member operator < that returns true if the amount is smaller than another one.
  • A member operator > that returns true if the amount is larger than another one. Write it using <.
  • A friend operator << that prints the amount followed by the word cents.

In main:

  • Create two amounts of your choice, and a third one that is their sum.
  • Multiply the third amount by a number of your choice with *=.
  • Print the three amounts with <<.
  • Print a message if the first amount is smaller than the second, using <.
  • Print a message if the second amount is larger than the first, using >.
Solution
#include <iostream>
using namespace std;

class Money {
private:
    int cents;

public:
    Money(int amount = 0) : cents(amount) {
    }
    Money operator+(const Money& other) const {
        return Money(cents + other.cents);
    }
    Money& operator*=(int times) {
        cents *= times;
        return *this;
    }
    bool operator<(const Money& other) const {
        return cents < other.cents;
    }
    bool operator>(const Money& other) const {
        return other < *this;
    }
    friend ostream& operator<<(ostream& out, const Money& m);
};

ostream& operator<<(ostream& out, const Money& m) {
    out << m.cents << " cents";
    return out;
}

int main() {
    Money a(250);
    Money b(1800);
    Money c = a + b;
    c *= 3;

    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    if (a < b)
        cout << "a is smaller" << endl;
    if (b > a)
        cout << "b is larger" << endl;
    return 0;
}

                    

Question 3

These are the functions of Money that matter for the lines in main:

class Money {
public:
    Money operator+(const Money& other) const;
    Money& operator++();
    Money operator++(int);
    friend ostream& operator<<(ostream& out, const Money& m);
    friend Money operator*(int times, const Money& m);
};

int main() {
    Money a;
    Money b;
    Money c = a + b;
    ++a;
    a++;
    cout << a;
    Money d = 3 * a;
    return 0;
}

For each line below, say which function it calls. Press Check answers when you are done.

Which function is called? 0 / 4 answered

This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.

1.Money c = a + b;line 13 of the program

Which function does this line call?

Answer for Money c = a + b;
2.a++;line 15 of the program

Which function does this line call?

Answer for a++;
3.cout << a;line 16 of the program

Which function does this line call?

Answer for cout << a;
4.Money d = 3 * a;line 17 of the program

Which function does this line call?

Answer for Money d = 3 * a;