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.
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.
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
centsof typeint, and a constructor with a default parameter of 0. - A member operator
+that returns the sum of two amounts as a newMoney. - A member operator
*=that multiplies the amount by anintand changes the object. It must work in a chain:(a *= 2) *= 3. - A member operator
<that returnstrueif the amount is smaller than another one. - A member operator
>that returnstrueif the amount is larger than another one. Write it using<. - A friend operator
<<that prints the amount followed by the wordcents.
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
>.
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.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
Money c = a + b;line 13 of the programWhich function does this line call?
a++;line 15 of the programWhich function does this line call?
cout << a;line 16 of the programWhich function does this line call?
Money d = 3 * a;line 17 of the programWhich function does this line call?