virtual functions/polymorphism

I need help with C ++ virtual functions/polymorphism assignment and I'm not understanding how to do these steps in a derived classes (Saving Account and Checking Account):

- In the virtual withdraw(double) function, check if the SavingAccount has sufficient balance to perform the withdraw and maintain the minBalance after the withdraw

- In the driver program invoke the print() function and perform a big amount withdraw through the Account pointer array.

What I've done so far with my code is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//ACCOUNT HEADER FILE (Base class)

#ifndef Account_h
#define Account_h

#include <iostream>
using namespace std;

class Account
{
	const int id;
	string name;
	double balance = 0.0;
	double accountAnnualInterestRate;
	static double annualInterestRate;

protected:
	void setBalance(double);

public:
	Account(int ID, const string& Name, double = 0.0 ); 
	~Account(); // Destructor

	// set function
	static void setAnnualInterestRate(double); // set the initial value as 5% 
	void setName(const string&);
	void setAccountAnnualInterestRate(double);

	// get functions
	unsigned int getID() const;
	string getName() const;
	double getBalance() const;
	static double getAnnualInterestRate(); 
	double getAccountAnnualInterestRate() const;
	double getMonthlyInterestRate() const;
	double getMonthlyInterest() const;

	virtual void withdraw(double) = 0; // pure virtual function,makes Account a abstract base class
	virtual void print() const;
	virtual void deposit(double);
};
#endif // Account_h  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// CHECKING ACCOUNT HEADER FILE (Derived class)

#ifndef CheckingAccount_h
#define CheckingAccount_h

#include "Account.h"
using namespace std;

class CheckingAccount : public Account

{
public:
	CheckingAccount(int, const string&, double); // Constructor
	~CheckingAccount(); // Destructor

	virtual void withdraw(double);
	virtual void print() const;
};

#endif // CheckingAccount_h 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  // SAVING ACCOUNT HEADER FILE (Derived class)

#ifndef SavingAccount_h
#define SavingAccount_h

#include "Account.h"
using namespace std;

class SavingAccount : public Account

{
	static double minBalance;

public:
	SavingAccount(int, const string&, double); // Constructor
	~SavingAccount(); // Destructor

	static void setMinBalance(double);
	static double getMinBalance();

	virtual void withdraw(double);
	virtual void print() const;
};

#endif // SavingAccount_h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//  ACCOUNT SOURCE CPP FILE (Base class)

#include <iostream>
#include <iomanip>
#include <string>
#include "Account.h"
using namespace std;

double Account::annualInterestRate = 0.00;
 
Account::Account( int ID, const string& Name, double Balance)
	:id(ID), name(Name), balance(Balance)
{
	accountAnnualInterestRate = annualInterestRate;
	
	setName(Name);
	setBalance(Balance);
}

Account::~Account() // destructor
{
	// leave blank, not deleting any objects but still kept
}

void Account::setBalance(double Balance)
{
	balance = Balance;
}

void Account::setAnnualInterestRate(double AnnualInterestRate)
{
	annualInterestRate = AnnualInterestRate;
}

void Account::setName(const string& Name)
{
	name = Name;
}

void Account::setAccountAnnualInterestRate(double AccountAnnualInterestRate)
{
	accountAnnualInterestRate = AccountAnnualInterestRate;
}

unsigned int Account::getID() const
{
	return id;
}

string Account::getName() const
{
	return name.substr(0, 30);
}

double Account::getBalance() const
{
	return balance;
}

double Account::getAnnualInterestRate()  
{
	return annualInterestRate;
}

double Account::getAccountAnnualInterestRate() const
{
	return accountAnnualInterestRate;
}

double Account::getMonthlyInterestRate() const
{
	return (accountAnnualInterestRate / 12);
}

double Account::getMonthlyInterest() const
{
	return balance*(annualInterestRate / 12);
}

void Account::deposit(double depositAmount)
{
	balance += depositAmount; 
}

void Account::print() const
{
	cout << "Account Number: " << getID() << endl;
	cout << "Account Name: " << getName().substr(0,30) << endl; // set within 30-character
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// CHECKING ACCOUNT SOURCE CPP FILE (Derived class)

#include <iostream>
#include <iomanip>
#include <string>
#include "Account.h"
#include "SavingAccount.h"
#include "CheckingAccount.h"

CheckingAccount::CheckingAccount(int ID, const string& Name, double Balance)
	:Account(ID, Name,Balance)
{
	
}

CheckingAccount::~CheckingAccount()
{
	// leave blank, not deleting any objects but still keep
}

void CheckingAccount::withdraw(double withdrawAmount)
{
	double minBalance;

	if (minBalance >= withdrawAmount)
	{
	minBalance -= withdrawAmount;
	}
	else
		throw invalid_argument("Insufficient Balance.");
}

void CheckingAccount::print() const
{
	Account::print();
	cout << "Account Annual Interest Rate(%): " << getAccountAnnualInterestRate() << endl;
	cout << "Account Current Balance($): " << getBalance() << endl;
	cout << "Perform withdraw with A Large Amount--" << endl;
	cout << "Insufficient Balance. The current balance is ($): " << getBalance() << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SAVING ACCOUNT SOURCE CPP FILE (Derived class)

#include <iostream>
#include <iomanip>
#include <string>
#include "Account.h"
#include "SavingAccount.h"


double SavingAccount::minBalance = 300.00;

SavingAccount::SavingAccount(int ID, const string& Name, double Balance)
	:Account(ID, Name, Balance)
{

}

SavingAccount::~SavingAccount()
{
	// leave blank, not deleting any objects but still kept
}

void SavingAccount::setMinBalance(double MinBalance)
{
	minBalance = MinBalance;
}

double SavingAccount::getMinBalance()
{
	return minBalance; 
}

void SavingAccount::withdraw(double withdrawAmount)
{
	// check if the SavingAccount has sufficient balance to perform the withdraw and maintain the minBalance after the withdraw
	if (minBalance >= withdrawAmount)
	{
		minBalance -= withdrawAmount;
	}
	else
		throw invalid_argument("Insufficient Balance.");
}

void SavingAccount::print() const
{
	Account::print();
	cout << "Account Annual Interest Rate(%): " << getAccountAnnualInterestRate()  << endl;
	cout << "Account Current Balance($): " << getBalance() << endl;
	cout << "SavingAccount should maintain a minimum balance $ " << getMinBalance() << endl;
	cout << "Perform withdraw with A Large Amount--" << endl;
	cout << "Insufficient Balance. The current balance is ($): " << getBalance() << endl;
	cout << "A Saving account needs to maintain a minimum balance ($): " << getMinBalance() << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// DRIVER PROGRAM TO TEST ACCOUNT CLASS 

#include <iostream>
#include <iomanip>
#include <string>
#include "Account.h"
#include "SavingAccount.h"
#include "CheckingAccount.h"
using namespace std;

int main()
{
	Account *AccountPtr[2]; 
	
	CheckingAccount checkingAccountObj(1002,"Snow White", 100.00);
	SavingAccount savingAccountObj(1003, "Green Arrow", 500.00);
	cout << fixed << setprecision(2);

	AccountPtr[0] = &checkingAccountObj;
	AccountPtr[1] = &savingAccountObj; 

	cout << "Perform polymorphism to display derived-class objects info through base-class Pointers:\n" << endl;
	
	cout << "Print Out Checking Account Information...." << endl;
	*AccountPtr = &checkingAccountObj;

	AccountPtr[0]->print();

	cout << endl;

	cout << "Print Out Saving Account Information...." << endl;
	*AccountPtr = &savingAccountObj;
	static_cast<Account*>(AccountPtr[1])->getAnnualInterestRate();
		
	AccountPtr[1]->print();
	
	cout << endl;

	return 0;


Last edited on
Registered users can post here. Sign in or register to post.