Initilization List + Inheritance Assistance/Help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

   #ifndef ACCOUNT_H
#define ACCOUNT_H


class Account
{
private:
	double accountBalance;

public:
	Account( double initialBalance = 0.0);
	bool bCredit( double);
	void credit( double );
	void debit( double );
	bool bDebit( double );
	void setBalance( double );
	double getBalance();
};

#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

 #ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H


class CheckingAccount : public Account
{
private:
	double fee_charged_per_transaction;
public:
	CheckingAccount(double initialBalance, double fee_amount);
	void credit (double);
	bool bDebit (double);
	void debit (double);

};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

 #ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H

#include "Account.h"

class SavingsAccount : public Account
{
private:
	double interestRate;

public:
	SavingsAccount(double initialBalance = 0.0, double interestRate = 0.0);
	double calculateInterest (double interestRate, double accountBalance);

};

#endif  


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

 // Lab 1: Account.cpp
// Member-function definitions for class Account.
#include <iostream>
#include "stdafx.h"
using namespace std;

#include "Account.h" // include definition of class Account

// Account constructor initializes data member balance
Account::Account( double initialBalance )
{
   // if initialBalance is greater than or equal to 0.0, set this value 
   // as the balance of the Account
   if ( initialBalance >= 0.0 )#include <iostream>
#include "stdafx.h"
using namespace std;

#include "Account.h" // include definition of class Account

Account::Account(double initialBalance)
{
	if (initialBalance <= 0.0)
		accountBalance = initialBalance;

	else
	{
		cout << "Initial balance is invalid!" << endl;
		accountBalance = 0.0;
	}
}

void Account::credit( double amount)
{
	accountBalance = amount + accountBalance;
}

bool Account::bDebit( double amount)
{
	if (amount > accountBalance)
	{
		cout << "Debit amount exceeded account balance!" << endl;
		return false;
	}

	else
	{
		accountBalance = accountBalance - amount;
		return true;
	}
}

void Account::setBalance( double newBalance )
{
	accountBalance = newBalance;
}

double Account::getBalance()
{
	return accountBalance;
}
 


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

 #include <iostream>
#include "stdafx.h"
using namespace std;

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount( double initialBalance, double fee_amount)
{
}

bool Account::bCredit (double fee_amount)
{
	if (true)
	{
		accountBalance = accountBalance - fee_amount;
		return true;
	}

	else
		return false;
}

void Account::debit (double amount)
{
	if (true)
	{
		if ( amount > accountBalance)
		{
			cout << "Debit amount exceeded account balance!" << endl;
			accountBalance = accountBalance + amount;
		}
		accountBalance = accountBalance + amount;
	}
}
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

 #include <iostream>
#include "stdafx.h"
using namespace std;

#include "SavingsAccount.h"

SavingsAccount::SavingsAccount( double initialBalance, double interestRate) : Account (interestRate)
{
}

double SavingsAccount::calculateInterest(double initerestRate, double accountBalance)
{
	return accountBalance * interestRate;
}


Here are the only two errors remaining:

1
2
3
4
5
6
 1>Account_Inheritance_Hierarchy.obj : error LNK2019: unresolved external symbol "public: void __thiscall CheckingAccount::credit(double)" (?credit@CheckingAccount@@QAEXN@Z) referenced in function _main

1>Account_Inheritance_Hierarchy.obj : error LNK2019: unresolved external symbol "public: void __thiscall CheckingAccount::debit(double)" (?debit@CheckingAccount@@QAEXN@Z) referenced in function _main

1>E:\SEM 2\MAT 373\Account_Inheritance_Hierarchy\Debug\Account_Inheritance_Hierarchy.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


(This has been edited from its original)
Last edited on
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
90
91
92

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

#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"

int main()
{
 Account account1( 50.0 ); // create Account object
 SavingsAccount account2( 25.0, .03 ); // create SavingsAccount object
 CheckingAccount account3( 80.0, 1.0 ); // create CheckingAccount object

 cout << fixed << setprecision( 2 );

 // display initial balance of each object
 cout << "account1 balance: $" << account1.getBalance() << endl;
 cout << "account2 balance: $" << account2.getBalance() << endl;
 cout << "account3 balance: $" << account3.getBalance() << endl;
 cout << endl;

 // attempt valid debits
 cout << "Attempting to debit $25.00 from account1." << endl;
 account1.debit( 25.0 ); // try to debit $25.00 from account1
 cout << "Attempting to debit $13.00 from account2." << endl;
 account2.debit( 13.0 ); // try to debit $13.00 from account2
 cout << "Attempting to debit $40.00 from account3." << endl;
 account3.debit( 40.0 ); // try to debit $40.00 from account3
 cout << endl;

 // display balances
 cout << "account1 balance: $" << account1.getBalance() << endl;
 cout << "account2 balance: $" << account2.getBalance() << endl;
 cout << "account3 balance: $" << account3.getBalance() << endl;
 cout << endl;

 // attempt valid credits
 cout << "Crediting $40.00 to account1." << endl;
 account1.credit( 40.0 ); // credit $40.00 to account1
 cout << "Crediting $65.00 to account2." << endl;
 account2.credit( 65.0 ); // credit $65.00 to account2
 cout << "Crediting $20.00 to account3." << endl;
 account3.credit( 20.0 ); // credit $20.00 to account3
 cout << endl;

 // display balances
 cout << "account1 balance: $" << account1.getBalance() << endl;
 cout << "account2 balance: $" << account2.getBalance() << endl;
 cout << "account3 balance: $" << account3.getBalance() << endl;
 cout << endl;

 // attempt invalid debits
 cout << "Attempting to debit $125.00 from account1." << endl;
 account1.debit( 125.0 ); // try to debit $125.00 from account1
 cout << "Attempting to debit $113.00 from account2." << endl;
 account2.debit( 113.0 ); // try to debit $113.00 from account2
 cout << "Attempting to debit $140.00 from account3." << endl;
 account3.debit( 140.0 ); // try to debit $140.00 from account3
 cout << endl;

 // display balances
 cout << "account1 balance: $" << account1.getBalance() << endl;
 cout << "account2 balance: $" << account2.getBalance() << endl;
 cout << "account3 balance: $" << account3.getBalance() << endl;
 cout << endl;

 // add interest to SavingsAccount object account2
 double interestEarned = account2.calculateInterest(25.0, .03);
 cout << "Adding $" << interestEarned << " interest to account2." 
 << endl;
 account2.credit( interestEarned );
 cout << "New account2 balance: $" << account2.getBalance() << endl;
 cout << endl;

 // create accounts with invalid initial balances
 Account account4( -50.0 ); // create Account object
 SavingsAccount account5( -25.0, .03 ); // create SavingsAccount object
 CheckingAccount account6( -80.0, 1.0 ); // create CheckingAccount object

 // display initial balance of each object
 cout << "account4 balance: $" << account4.getBalance() << endl;
 cout << "account5 balance: $" << account5.getBalance() << endl;
 cout << "account6 balance: $" << account6.getBalance() << endl;
 cout << endl; 

 return 0;
} // end main




(Could not put Main in last post)
Last edited on
Look at what your compiler messages are telling you.

The first one is telling you that at line 10 of savingsaccount.cpp, you have a problem because you're attempting to define a function in an invalid scope. Lo and behold, if we look at lines 8 - 10 of savingsaccount.cpp, we see:

1
2
3
4
5
SavingsAccount::SavingsAccount(double initialBalance, double interestRate) : Account (interestRate)
{
	double SavingsAccount::calculateInterest(double balance, double interestRate)
	{

You're attempting to define one function inside another. You're trying to define calculateInterest() inside the constructor. This is not legal in C++.

If you look at your other compiler errors, they'll tell you where your other problems are.
^Thanks for the tip! that solved a good portion of the problems but I'm struggling now with JUST the CheckingAccount.cpp file.

This is what it says:

Derived class CheckingAccount should inherit from base class Account and include an additional data member of type double that represents the fee charged per transaction.
Checking-Account's constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine member functions credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount's versions of these functions should invoke the base-class Account version to perform the updates to an account balance. CheckingAccount's debit function should charge a fee only if money is actually withdrawn(i.e., the debit amount does not exceed the account balance). [Hint: Define Account's debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether fee should be charged.]
After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's credit function.

The bold is where I'm getting confused. Can you try to explain it better? I will add the code once I have worked on it.
Last edited on
Topic archived. No new replies allowed.