Write a Function with the Header

Total Page:16

File Type:pdf, Size:1020Kb

Write a Function with the Header

BTP200 Mid Term Test 2 April 2 2004

Name ______Student Number ______

1 – (10 Marks) Design and code a class named Account that holds information for a bank account. Upon instantiation, an Account object receives an int holding the account number, a null-terminated C-string holding the name of the account holder and a double holding the opening balance. If the account number is not positive or the balance is negative, the object sets itself to an empty state.

For your Account objects to work properly, you will need to allocate dynamic memory for the name of the account holder. Design your class so that copies of an object are not permitted and one Account object cannot be assigned to another Account object.

Your class should also include the following member functions:

o void deposit(double) – receives an amount and deposits that amount into the account. o bool withdraw(double) – receives an amount and, if there are sufficient funds, withdraws that amount from the account and returns true, but, if there are insufficient funds, does nothing and returns false. o double acctBalance( ) const – returns the balance in the account. o void display( ) const – displays to standard output the account information in the following order: the account number left justified and 0 filled in a field of 11, the balance right justified and dot (‘.’) filled in a field of 10 and finally the name of the account holder left justified. If the object is empty, this function should display some reasonable message. For example, an output may look something like 00123456789...1000.00 Harry Potter

Page 1 of 6 BTP200 Mid Term Test 2 April 2 2004

2 – (10 Marks) Derive from your Account class another class named Savings that holds information about a savings account. Upon instantiation, a Savings object receives an int holding the account number, a null-terminated C-string holding the name of the account holder, a double holding the opening balance, a double holding the annual interest rate as a percent and a null-terminated C-string holding the opening date in dd/mm/yyyy format. If the rate is negative or greater than 60% (the criminal code rate) or the opening date is invalid, the object sets itself to an empty state.

Your Savings class should include the following member functions: o bool deposit(double, const char*) – receives the amount to be deposited and a null-terminated string holding the deposit date. If the deposit date is valid and postdates the most recent transaction date, this function deposits the amount into the account and returns true. If the deposit date is invalid or predates the most recent transaction date, this function does not deposit the amount and returns false. o bool withdraw(double, const char*) – receives the amount to be withdrawn and a null-terminated string holding the withdrawal date. If the withdrawal date is valid and postdates the most recent transaction date and if there are sufficient funds to cover the withdrawal, this function withdraws the amount and returns true. If the withdrawal date is valid but there are insufficient funds to cover the withdrawal, this function accepts the date as the most recent transaction date, doesn’t withdraw funds and returns false. Otherwise, this function does nothing and returns false. o void compound( ) – adds all accrued interest to the account balance. o void display( ) const – displays to standard output the account information in the following order: the date of the most recent transaction, the account number left justified and 0 filled in a field of 11, the balance right justified and dot (‘.’) filled in a field of 10, the name of the account holder left justified and the interest accrued until the date of the most recent transaction along with that date. For example, the output might look something like 01/07/2003 00123456789...1000.00 Harry Potter (10.32 accrued interest)

Your Savings object tracks the interest accrued and compounds that interest upon request.

Page 2 of 6 BTP200 Mid Term Test 2 April 2 2004

[Hint: before every transaction calculate the interest accrued to the transaction date, add that interest to previously accrued interest and change the date of the most recent transaction to the current transaction date.]

To help in your design, you have been granted access to the code for a class named Date that holds a calendar date. The class definition is in Date.h You do not need to code this class.

The Date class contains the following member functions that you may use in your design of Savings: o Date() – creates an empty date. o Date(const char*) – receives a null-terminated string holding a date in dd/mm/yyyy format and stroes the date if it is valid; otherwise sets an empty state. o int daysTo(const Date& ) – receives a Date and returns the number of days from the current date to the date received. o int daysInYear( ) – returns the number of days in the year of the current date. o bool valid() – returns true if the current date is a valid calendar date, false if not.

The Date class also includes the following helper function that you may use in your output design for a Savings object: o ostream& operator<<(ostream&, const Date&) – sends the date object to an ostream in dd/mm/yyyy format.

Minimum Requirements for the Savings class: If you don’t have sufficient time, you may assume that the Date information passed to the member functions deposit and withdraw will always be valid and will always postdate the most recent transaction date. But if you have sufficient time and add logic to these two member functions to handle invalid dates or predates, you will receive bonus marks for your extra effort.

Page 3 of 6 BTP200 Mid Term Test 2 April 2 2004

Example Consider the following main program: #include using namespace std; #include "Savings.h" int main() { Savings savings(123456789,"Harry Potter", 1000.00, 10, "1/1/2003");

savings.display(); savings.deposit(1000, "1/1/2004"); savings.display(); savings.withdraw(500, "1/3/2004"); savings.display(); savings.compound(); savings.display(); savings.deposit(450, "1/6/2003"); // date predates most recent // transaction date savings.display(); savings.withdraw(2000, "1/12/2004"); // insufficient funds savings.display(); savings.compound(); savings.display();

return 0; }

This program would produce something like 01/01/2003 00123456789 ...1000.00 Harry Potter (0.00 accrued interest) 01/01/2004 00123456789 ...2000.00 Harry Potter (100.00 accrued interest) 01/03/2004 00123456789 ...1500.00 Harry Potter (132.79 accrued interest) 01/03/2004 00123456789 ...1632.79 Harry Potter (0.00 accrued interest) **Deposit rejected - date specified predates most recent transaction** 01/03/2004 00123456789 ...1632.79 Harry Potter (0.00 accrued interest) **Insufficient funds - withdrawal rejected** 01/12/2004 00123456789 ...1632.79 Harry Potter (122.68 accrued interest) 01/12/2004 00123456789 ...1755.47 Harry Potter (0.00 accrued interest)

Page 4 of 6 BTP200 Mid Term Test 2 April 2 2004

3 – (20 Marks) What is the exact output of the following program? You will not get full marks if you do not include your rough work. #include using namespace std; #include class T { char* t; int n; void myo(char* s) { t = new char[strlen(s) + 1]; strcpy(t, s); } public: T(char* s) {myo(s); n = 0;} T(const T& t) {myo(t.t); n = t.n; cout << "...\n";} ~T() {delete[]t; cout << "^^^\n";} void operator=(const T& s) { delete[]t; n = s.n; myo(s.t); cout << "ooo\n"; } void boo() {n = 0;} bool end() {return n == strlen(t);} int see(int m) { int j; for (j = 0; j < m && t[n]; j++) cout << t[n++]; return (t[n]) ? j : 0; } }; class P : public T { char* moo; int g; int w; int n; void myo(char* s) { n = strlen(s); moo = new char[n + 1]; strcpy(moo,s); } void lin() { for(int i = 0; i < w; i++) cout << '='; cout << '\n'; } public: P(int nl, int up, char* s, char* t) : T(s) { myo(t); g = nl; w = up; } void dow(int u) {w = u;} void acr(int p) {g = p;}

Page 5 of 6 BTP200 Mid Term Test 2 April 2 2004

P(const P& p) : T(p) { myo(p.moo); g = p.g; w = p.w; } ~P() {delete[]moo; lin();} void operator=(const P& p) { T::operator=(p); delete[]moo; g = p.g; w = p.w; myo(p.moo); cout << "===\n"; } void see(int e) { bool ok = false; int k, p = 0; if (!end()) { do { k = 0; for (int i = 0; i < (n-1)/w + 1; i++) { for (int j = 0; j < w && moo[k]; j++) cout << moo[k++]; cout << '\n'; } lin(); for (int i = 0; i < g && !ok; i++) { ok = (T::see(w) == 0); cout << '\n'; } cout << '\n'; p++; } while(p < e && !ok); } } }; int main() { P a(2, 6, "Inheritance, Encapsulation and Polymorphism","BTP200");

a.see(1); P b = a; b.see(1); a = b; a.see(2); a.dow(8); a.acr(2); a.boo(); a.see(1); a.dow(3); a.see(1);

return 0; }

Page 6 of 6

Recommended publications