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.
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.
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 wordsavingsand the balance.
CheckingAccount has:
- A
feeand a constructor that takes the balance and the fee. monthEnd(), which takes the fee out of the balance.print(), which prints the wordcheckingand 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 withnew: one savings account, one checking account and one plainAccount. - In one loop, call
monthEnd()andprint()on every account. - Print the total.
- Delete every account.
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.
This question needs JavaScript to check your answers. You can still read the program and work out the answers on paper.
a->monthEnd();line 21 of the programa is a pointer of type Account*, and it points to a Savings object. Which monthEnd runs?
a->info();line 22 of the programThe same pointer a. Which info runs?
r.monthEnd();line 25 of the programr is a reference of type Account&, and it refers to the Savings object s. Which monthEnd runs?
delete a;line 26 of the programAccount has a virtual destructor, and a points to a Savings object created with new. Which destructors run?
void monthEnd() override;line 16 of the programChecking is also derived from Account, and this is its declaration of monthEnd. Does the line compile?