Help with HW

I am trying to compile this 7 file program but I've hit some road block. Here is the list of files I will be posting;

BankAccountMain.cpp
BankAccount.h
BankAccount.cpp
CheckingAccount.h
CheckingAccount.cpp
SavingsAccount.h
SavingsAccount.cpp

Ok and now for the coding...

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
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

void main(void)
{
int i = 0;
int newAccountNumber = 0;
double depositAmount = 0.0;
double withdrawAmount = 0.0;
double newInterestRate = 0.0;

srand(GetTickCount());					//Seed the random number generator

//BankAccount account1;

CheckingAccount account1;
SavingsAccount account2;

cout << "Enter a six digit integer for your new savings account number:  ";
cin >> newAccountNumber;
account2.setAccountNumber(newAccountNumber);
cout << endl;

cout << "Retrieving new savings account number" << endl;
cout << "New Savings Account Number:  " << account2.getAccountNumber() << endl << endl;

cout << "Set savings account interest rate" << endl;
cin >> newInterestRate;
account2.setInterestRate(newInterestRate);

cout << "Savings account balance for account # " << account2.getAccountNumber() <<
	" is $" << account2.getAccountBalance() << endl << endl;
cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
	<< " days" << endl << endl;

cout << showpoint << fixed << setprecision(2);

account2.depositMoney(100.0);
cout << "Savings account balance for account # " << account1.getAccountNumber() <<
	" is $" << account2.getAccountBalance() << endl;
cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
	<< " days" << endl << endl;

for(i = 0; i < 10; i++)
	{
	cout << "Withdraw money from savings account" << endl;
	if(account2.withdrawMoney(20.0) == false)
		cout << "Withdrawal denied, insufficient funds" << endl;	

	cout << "Savings account balance for account # " << account2.getAccountNumber() <<
		" is $" << account2.getAccountBalance() << endl;

	cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
		<< " days" << endl << endl;
	

}
}


The first class, "BankAccount"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//BankAccount.h
#ifndef bankaccount_H
#define bankaccount_H
#include <string>
using namespace std;

class BankAccount //Names the class
{ 
private: //variables found in main program
double newAccountNumber; 
double AccountBalance;

public: //Getter and setter functions.
BankAccount(int m, double amt, bool display); 
~BankAccount(); //Destructor
void setAccountNumber(int m);
int getAccountNumber(void);
double getAccountBalance (void);
void depositMoney (double amt);
bool withdrawMoney (bool display);
};
#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
//BankAccount.h
#include "BankAccount.h"
#include <iostream>
using namespace std;

//constructor
BankAccount::BankAccount(int num, double amt, bool display)
{
	newAccountNumber = num;
	AccountBalance = amt;

}
//functions
void BankAccount::setAccountNumber(int m)//a
{
	newAccountNumber = m;
}
int BankAccount::getAccountNumber(void) //b
{
	int num;
		cout <<"Enter Account Number"<<endl;
		cin >> num;
		return num;
}
double BankAccount::getAccountBalance(void)//c
{ 
	return AccountBalance;
}
void BankAccount::depositMoney(double amt) //d
{
		return amt;
}

bool BankAccount::withdrawMoney(bool display) //e
{
		return display;
}
BankAccount::~BankAccount() //Implementation of destructor from class.
{}
The second class, "Checking Account"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//CheckingAccount.h
#ifndef checkingaccount_H
#define checkingaccount_H
#include <string>
using namespace std;

class CheckingAccount //Names the class
{ 
private: //variables found in main program
 
int transactionCount;



public: //Getter and setter functions.
CheckingAccount(int m, bool display); 
~CheckingAccount(); //Destructor

void withdrawMoney(int m, bool display);
};
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//CheckingAccount.h
#include "CheckingAccount.h"
#include <iostream>
using namespace std;

//constructor
CheckingAccount::CheckingAccount(int m, bool display)
{
	
	transactionCount = m;
	
}
//functions
void CheckingAccount::withdrawMoney(int m, bool display)
{
	transactionCount=m;
}
CheckingAccount::~CheckingAccount() //Implementation of destructor from class.
{}
And finally the 3rd class, the "SavingsAccount"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//SavingsAccount.h
#ifndef savingsaccount_H
#define savingsaccount_H
#include <iomanip>

using namespace std;

class SavingsAccount //Names the class
{ 
private: //variables found in main program
double newInterestRate;
int numberOfDays;
double earnedInterest; 


public: //Getter and setter functions.
SavingsAccount(double i, int b, double c); 
~SavingsAccount(); //Destructor
void setInterestRate(double i);
double getAccountBalance();
double getInterestEarned();
int getNumberOfDays();
};
#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
//SavingsAccount.cpp
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
SavingsAccount::SavingsAccount(double i, int b, double c)
{
	newInterestRate = i;
	numberOfDays = b;
	earnedInterest = c;
}
//functions
 void SavingsAccount::setInterestRate(double i)
 {
 	newInterestRate = i;
 }

double SavingsAccount::getAccountBalance()
 { 
 	double dRate = newInterestRate/365;
 	numberOfDays = rand()%8;
 	earnedInterest = balance * (earnedInterest * dRate);
 	balance += earnedInterest;
 	return balance;
 } 
 
 double SavingsAccount::getInterestEarned()
 {
 	return earnedInterest;
 }

int SavingsAccount::getNumberOfDays()
 {
 	return numberOfDays;
 }
 
SavingsAccount::~SavingsAccount()//Implementation of destructor from class.
{}
Here is my output/compiling errors...

1>------ Build started: Project: Wk3, Configuration: Debug Win32 ------
1>Build started 2/6/2013 12:20:55 AM.
1>InitializeBuildStatus:
1>  Touching "Debug\Wk3.unsuccessfulbuild".
1>ClCompile:
1>  SavingsAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(23): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(24): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(25): error C2065: 'balance' : undeclared identifier
1>  BankAccountMain.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(26): error C2512: 'CheckingAccount' : no appropriate default constructor available
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(27): error C2512: 'SavingsAccount' : no appropriate default constructor available
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(31): error C2039: 'setAccountNumber' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(35): error C2039: 'getAccountNumber' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(41): error C2039: 'getAccountNumber' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(48): error C2039: 'depositMoney' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(49): error C2039: 'getAccountNumber' : is not a member of 'CheckingAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(8) : see declaration of 'CheckingAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(57): error C2039: 'withdrawMoney' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(60): error C2039: 'getAccountNumber' : is not a member of 'SavingsAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9) : see declaration of 'SavingsAccount'
1>  BankAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.cpp(31): error C2562: 'BankAccount::depositMoney' : 'void' function returning a value
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.h(19) : see declaration of 'BankAccount::depositMoney'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.49
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any and all help is much appreciated.
Which errors are you having trouble with? Most of them seem pretty self-explanatory.
To be honest I'm not sure why I'm getting these errors in the first place. I thought the code looked solid, and individually they compile.
OK the default constructor I get, I did miss that. The others I'm not so sure on. How do I get the functions to become members of the class? I thought I did that.
Bump
As an example, lets look at the error concerning setAccountNumber(). The error says that it is not a member of SavingsAccount, and upon inspection I see that is the case; the members of SavingsAccount are those listed on lines 11-13 and 17-22 of SavingsAccount.hpp. There is no mention of the method setAccountNumber().

I have a strong suspicion of your intent, but see if my hint there helps you see the issue.
closed account (Dy7SLyTq)
you didn't declare a balance variable but still tried to use it
As an example, lets look at the error concerning setAccountNumber(). The error says that it is not a member of SavingsAccount, and upon inspection I see that is the case; the members of SavingsAccount are those listed on lines 11-13 and 17-22 of SavingsAccount.hpp. There is no mention of the method setAccountNumber().

I have a strong suspicion of your intent, but see if my hint there helps you see the issue


My setAccountNumber() function is in my BankAccount class, there is no mention of it in the Savings Account class. So now I'm really confused. Are you saying I need to link the classes together somehow?
Last edited on
If that is what you are hinting to then this is what I got...

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
//SavingsAccount.h
#ifndef savingsaccount_H
#define savingsaccount_H
#include <iomanip>
#include "BankAccount.h"
using namespace std;

class SavingsAccount //Names the class
class SavingsAccount: public BankAccount
{ 
private: //variables found in main program
double newInterestRate;
int numberOfDays;
double earnedInterest; 


public: //Getter and setter functions.
SavingsAccount();
SavingsAccount(double i, int b, double c); 
~SavingsAccount(); //Destructor
void setInterestRate(double i);
double getAccountBalance();
double getInterestEarned();
int getNumberOfDays();
};
#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
//SavingsAccount.cpp
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
SavingsAccount::SavingsAccount()
{
	newInterestRate = 0;
	numberOfDays = 0;
	earnedInterest = 0;
}
SavingsAccount::SavingsAccount(double i, int b, double c)
{
	newInterestRate = i;
	numberOfDays = b;
	earnedInterest = c;
}
//functions
 void SavingsAccount::setInterestRate(double i)
 {
 	newInterestRate = i;
 }

double SavingsAccount::getAccountBalance()
 { 
 	double dRate = newInterestRate/365;
 	numberOfDays = rand()%8;
 	earnedInterest = balance * (earnedInterest * dRate);
 	balance += earnedInterest;
 	return balance;
 } 
 
 double SavingsAccount::getInterestEarned()
 {
 	return earnedInterest;
 }

int SavingsAccount::getNumberOfDays()
 {
 	return numberOfDays;
 }
 
SavingsAccount::~SavingsAccount()//Implementation of destructor from class.
{}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//CheckingAccount.h
#ifndef checkingaccount_H
#define checkingaccount_H
#include <string>
#include "BankAccount.h"
using namespace std;

class CheckingAccount //Names the class
class CheckingAccount: public BankAccount
{ 
private: //variables found in main program
 
int transactionCount;

public: //Getter and setter functions.
CheckingAccount();
CheckingAccount(int m, bool display); 
~CheckingAccount(); //Destructor

void withdrawMoney(int m, bool display);
};
#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
//CheckingAccount.h
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
CheckingAccount::CheckingAccount()
{
	
	transactionCount = 0;
	
}
CheckingAccount::CheckingAccount(int m, bool display)
{
	
	transactionCount = m;
	
}
//functions
void CheckingAccount::withdrawMoney(int m, bool display)
{
	transactionCount=m;
}
CheckingAccount::~CheckingAccount() //Implementation of destructor from class.
{}
Here is output errors...

1>------ Build started: Project: Wk3, Configuration: Debug Win32 ------
1>Build started 2/7/2013 2:50:21 AM.
1>InitializeBuildStatus:
1>  Touching "Debug\Wk3.unsuccessfulbuild".
1>ClCompile:
1>  SavingsAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C2236: unexpected 'class' 'CheckingAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C3381: 'CheckingAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C2236: unexpected 'class' 'SavingsAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C3381: 'SavingsAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(31): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(32): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(33): error C2065: 'balance' : undeclared identifier
1>  CheckingAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C2236: unexpected 'class' 'CheckingAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C3381: 'CheckingAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C2236: unexpected 'class' 'SavingsAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C3381: 'SavingsAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>  BankAccountMain.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C2236: unexpected 'class' 'CheckingAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C3381: 'CheckingAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C2236: unexpected 'class' 'SavingsAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C3381: 'SavingsAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(57): warning C4305: 'argument' : truncation from 'double' to 'bool'
1>  BankAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C2236: unexpected 'class' 'CheckingAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(9): error C3381: 'CheckingAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C2236: unexpected 'class' 'SavingsAccount'. Did you forget a ';'?
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C3381: 'SavingsAccount' : assembly access specifiers are only available in code compiled with a /clr option
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.cpp(39): error C2562: 'BankAccount::depositMoney' : 'void' function returning a value
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.h(20) : see declaration of 'BankAccount::depositMoney'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.64
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
closed account (Dy7SLyTq)
ok so if im reading ur output right, you forgot a semicolon on line 9 of checking account.cpp. you still havent declared balance. you forgot a semicolon for savings account and you set bankaccount::depositmoney() to void but you still have it returning a value
1
2
class CheckingAccount //Names the class
class CheckingAccount: public BankAccount

The first line is unnecessary and should be removed.

EDIT: also for the SavingsAccount class.
Last edited on
It's more than unnecessary, it's an error. If it had a semicolon, then it would just be unnecessary...

Start by removing those //Names the class lines and adding a member variable ('balance') to store a balance in the SavingsAccount class. Hint: be sure to initialize balance in the constructor.
Last edited on
OK so I think one of my issues was I had each class as its own base class and not as inherited classes of the base class which in this case is 'BankAccount'. I also found some issues with my functions as pointed out but some of you. Here are the changes that I made...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//CheckingAccount.h
#pragma once 
#include <string>
using namespace std;

class CheckingAccount: public BankAccount //inherits base class
{ 
private: //variables found in main program
 
int transactionCount;



public: //Getter and setter functions.
CheckingAccount();
CheckingAccount(int m, bool display); 
~CheckingAccount(); //Destructor

void withdrawMoney(int m, bool display);
};


I did the same treatment to the checking account and savings account classes. I know I still have the 'balance' not being defined so I am trying to figure out how to correct that. (Instructor gave me a UML to go by). Here is the output now.
1>------ Build started: Project: Wk3, Configuration: Debug Win32 ------
1>Build started 2/8/2013 2:37:46 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Wk3.unsuccessfulbuild".
1>ClCompile:
1>  SavingsAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.h(9): error C2504: 'BankAccount' : base class undefined
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(31): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(32): error C2065: 'balance' : undeclared identifier
1>f:\my documents\visual studio 2010\projects\wk3\wk3\savingsaccount.cpp(33): error C2065: 'balance' : undeclared identifier
1>  CheckingAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\checkingaccount.h(7): error C2504: 'BankAccount' : base class undefined
1>  BankAccountMain.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccountmain.cpp(57): warning C4305: 'argument' : truncation from 'double' to 'bool'
1>  BankAccount.cpp
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.cpp(34): error C2562: 'BankAccount::depositMoney' : 'void' function returning a value
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.h(20) : see declaration of 'BankAccount::depositMoney'
1>f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.cpp(38): error C2511: 'bool BankAccount::withdrawMoney(double)' : overloaded member function not found in 'BankAccount'
1>          f:\my documents\visual studio 2010\projects\wk3\wk3\bankaccount.h(8) : see declaration of 'BankAccount'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.12
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
OK just as a thread update. I finally was able to make the program compile. I fixed the undeclared identifier 'balance', by renaming 'AccountBalance' to 'balance'. I also added #include BankAccount to the derived classes to define it, and I changed void to double to allow the function to return a value.

Thanks to everyone who chimed in to help me.
Topic archived. No new replies allowed.