Course Materials

OOP - Week 7: Programming Questions

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

Filter:
Sort:

Question 1

Trace the following program. Savings and Checking are derived from Account. Every constructor and destructor prints a message.

#include <iostream>
using namespace std;

class Account {
public:
    Account() {
        cout << "Account" << endl;
    }
    virtual ~Account() {
        cout << "~Account" << endl;
    }
    void info() const {
        cout << "account info" << endl;
    }
    virtual void monthEnd() const {
        cout << "account month end" << endl;
    }
};

class Savings : public Account {
public:
    Savings() {
        cout << "Savings" << endl;
    }
    ~Savings() {
        cout << "~Savings" << endl;
    }
    void info() const {
        cout << "savings info" << endl;
    }
    void monthEnd() const override {
        cout << "add interest" << endl;
    }
};

class Checking : public Account {
public:
    Checking() {
        cout << "Checking" << endl;
    }
    ~Checking() {
        cout << "~Checking" << endl;
    }
    void monthEnd() const override {
        cout << "take fee" << endl;
    }
};

int main() {
    Account* accounts[3];
    accounts[0] = new Savings();
    accounts[1] = new Checking();
    accounts[2] = new Account();
    for (int k = 0; k < 3; k++) {
        accounts[k]->info();
        accounts[k]->monthEnd();
    }
    Savings s;
    s.info();
    delete accounts[0];
    cout << "end" << endl;
    delete accounts[1];
    delete accounts[2];
    return 0;
}

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

Solution
Account
Savings
Account
Checking
Account
account info
add interest
account info
take fee
account info
account month end
Account
Savings
savings info
~Savings
~Account
end
~Checking
~Account
~Account
~Savings
~Account

                    

Question 2

The class Account is given:

#include <iostream>
using namespace std;

class Account {
private:
    double balance;

public:
    Account(double b) : balance(b) {
    }
    virtual ~Account() {
    }
    void deposit(double amount) {
        if (amount > 0)
            balance += amount;
    }
    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            return true;
        }
        return false;
    }
    double getBalance() const {
        return balance;
    }
    virtual void monthEnd() {
    }
    virtual void print() const {
        cout << "account " << balance << endl;
    }
};

Write the classes SavingsAccount and CheckingAccount, and a main that uses them. Put them after the class above.

SavingsAccount has:

  • A rate, a percent, and a constructor that takes the balance and the rate.
  • monthEnd(), which adds the interest of the month to the balance: the balance times the rate, divided by 100.
  • print(), which prints the word savings and the balance.

CheckingAccount has:

  • A fee and a constructor that takes the balance and the fee.
  • monthEnd(), which takes the fee out of the balance.
  • print(), which prints the word checking and the balance.

Both classes must work through a pointer of type Account*.

In the file, write a function total that receives an array of Account* and its size, and returns the sum of the balances. Its header is double total(Account* accounts[], int size).

In main:

  • Create an array of 3 Account*, and fill it with new: one savings account, one checking account and one plain Account.
  • In one loop, call monthEnd() and print() on every account.
  • Print the total.
  • Delete every account.
Solution
#include <iostream>
using namespace std;

class Account {
private:
    double balance;

public:
    Account(double b) : balance(b) {
    }
    virtual ~Account() {
    }
    void deposit(double amount) {
        if (amount > 0)
            balance += amount;
    }
    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            return true;
        }
        return false;
    }
    double getBalance() const {
        return balance;
    }
    virtual void monthEnd() {
    }
    virtual void print() const {
        cout << "account " << balance << endl;
    }
};

class SavingsAccount : public Account {
private:
    double rate;

public:
    SavingsAccount(double b, double r) : Account(b), rate(r) {
    }
    void monthEnd() override {
        deposit(getBalance() * rate / 100);
    }
    void print() const override {
        cout << "savings " << getBalance() << endl;
    }
};

class CheckingAccount : public Account {
private:
    double fee;

public:
    CheckingAccount(double b, double f) : Account(b), fee(f) {
    }
    void monthEnd() override {
        withdraw(fee);
    }
    void print() const override {
        cout << "checking " << getBalance() << endl;
    }
};

double total(Account* accounts[], int size) {
    double sum = 0;
    for (int k = 0; k < size; k++)
        sum += accounts[k]->getBalance();
    return sum;
}

int main() {
    Account* accounts[3];
    accounts[0] = new SavingsAccount(1000, 2);
    accounts[1] = new CheckingAccount(500, 15);
    accounts[2] = new Account(200);

    for (int k = 0; k < 3; k++) {
        accounts[k]->monthEnd();
        accounts[k]->print();
    }
    cout << "total " << total(accounts, 3) << endl;

    for (int k = 0; k < 3; k++)
        delete accounts[k];
    return 0;
}

                    

Question 3

Savings and Checking are derived from Account. monthEnd is virtual, info is not, and the destructor of Account is virtual:

class Account {
public:
    virtual ~Account();
    void info() const;
    virtual void monthEnd() const;
};

class Savings : public Account {
public:
    void info() const;
    void monthEnd() const override;
};

class Checking : public Account {
public:
    void monthEnd() override;
};

int main() {
    Account* a = new Savings;
    a->monthEnd();
    a->info();
    Savings s;
    Account& r = s;
    r.monthEnd();
    delete a;
    return 0;
}

For each item below, say what happens. Press Check answers when you are done.

Which version runs? 0 / 5 answered

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

1.a->monthEnd();line 21 of the program

a is a pointer of type Account*, and it points to a Savings object. Which monthEnd runs?

Answer for a->monthEnd();
2.a->info();line 22 of the program

The same pointer a. Which info runs?

Answer for a->info();
3.r.monthEnd();line 25 of the program

r is a reference of type Account&, and it refers to the Savings object s. Which monthEnd runs?

Answer for r.monthEnd();
4.delete a;line 26 of the program

Account has a virtual destructor, and a points to a Savings object created with new. Which destructors run?

Answer for delete a;
5.void monthEnd() override;line 16 of the program

Checking is also derived from Account, and this is its declaration of monthEnd. Does the line compile?

Answer for void monthEnd() override;