Inheritance: cant get a function to work properly

Hello fellow programmers!

I have an assignment that isn't due till a few days, I'm just trying to get a head start.

So I have a program that is supposed to make "CheckingAccount.h" inherit from "BankAccount.h". I have written up the code and I am error free! But one of my functions for "setTransactionFee" or "getTransactionFee" will not do its job correctly. I am not expecting someone to write out the code for the function(even though that would be awesome!), but any ideas or help would be appreciated.

Here is what the output should look like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
         Welcome to your bank

1. Make a deposit
2. Make a withdrawal
3. Set the account name
4. Set the account number
5. Set the transaction fee
6. Display account information
7. Exit

Please enter your choice: 6

Account Information
Number:23456
Name:Joe Smith
Balance:49.00
Transaction Fee:1.00



This is the code I have written up already..
Following includes:
-Bank.cpp
-BankAccount.h (SUPER CLASS)
-CheckingAccount.h (CHILD CLASS)

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
/**********************BANK.CPP********************/
#include <iostream>
#include <iomanip>
#include <string>
#include "BankAccount.h"
#include "CheckingAccount.h"

using namespace std;

void displayMenu();
double getValidData();


//***************************************************
//                main
//***************************************************
int main()
{
	BankAccount CheckingAccount;  //declare and create with constructor
	int choice = 0;
	int iValue = 0;
	string name = "unknown";
	
	do {
		displayMenu(); 
	    cin >> choice;
		cout << endl;
		switch (choice)
		{
			case 1 : cout << "Deposit amount. " ;
					 CheckingAccount.deposit(getValidData());
					 break;
			case 2 : cout << "Withdraw amount. " ;
					 CheckingAccount.withdraw(getValidData());
					 break;
			case 3 : cout << "Enter the name: ";
					 cin.ignore();  //skips the newline char
					 getline(cin,name);  //reads an entire line
					 CheckingAccount.setName(name);
					 break;
			case 4 : cout << "Account number. " ;
					 CheckingAccount.setAcctNumber(static_cast<int>(getValidData( )) );
					 break;
			case 5 : CheckingAccount.displayData();       //display all account data
					 break;
			case 6 : cout << "\nBank is closed.\n" ;
					 break;
			default : cout << "Invalid entry.";
		}//switch

	}while (choice != 6);
	

	return 0;

}//end of main

//This function displays the user's menu on the screen.
//This is NOT a BankAccount member function
void displayMenu()
{
	cout << "\n         Welcome to your bank \n\n";
	cout << "1. Make a deposit\n";
	cout << "2. Make a withdrawal\n";
	cout << "3. Set the account name\n";
	cout << "4. Set the account number\n";
	cout << "5. Display account information\n";
	cout << "6. Exit\n\n";
	cout << "Please enter your choice: ";
}

//This function prompts the user for a number
//and checks to see if 0 or greater.  Returns 
//a valid number

double getValidData()
{
	double amount = 0;
	cout<< "Enter value: " ;  //deposit or withdraw amt or account number
	cin >> amount;
	while (amount < 0 )
	{
		cout << "Enter value: (0 or greater): " ; 
		cin >> amount;
	}
	return amount;
}


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/****************************BANKACCOUNT.h************/

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


//BankAccount class declaration
class BankAccount
{
	protected:
			int acctNumber;
			string name;
			double balance;

	public:
			BankAccount();
			BankAccount(int aNum, string aName, double amount);
			void setAcctNumber(int aNum); 
			void setName(string aName); 
			void setBalance(double amount); 
			int getAcctNumber(); 
			string getName(); 
			double getBalance();
			void deposit(double amount);
			void withdraw(double amount); 
			void displayData(); 

};//End of BankAccount class declaration


//Member function implementation section

// Constructor function. Initializes acctNumber to 0, name to “Unknown” and 
// balance to 0.0.
BankAccount::BankAccount()
{
	acctNumber = 0;
	name = "Unknown";
	balance = 0.0;
}

//Overloaded constructor. overloaded constructor function. 
//Initializes acctNumber to aNum, name to aName and balance to amount.  
BankAccount::BankAccount(int aNum, string aName, double amount)
{
	acctNumber = aNum;
	name = aName;
	balance = amount;
}

//This function copies the argument passed into the parameter to 
//the private member variable acctNumber.
void BankAccount::setAcctNumber(int aNum)
{
	acctNumber = aNum; 
}

//This function copies the argument passed into the parameter to 
//the private member variable name.
void BankAccount::setName(string aName)
{
	name = aName; 
}

//This function copies the argument passed into the parameter to 
//the private member variable balance.
void BankAccount::setBalance(double amount)
{
	balance = amount; 
}

//This function returns the value of the private member variable acctNumber.
int BankAccount::getAcctNumber()
{
	return acctNumber; 
}

//This function returns the value of the private member variable name.
string BankAccount::getName()
{
	return name;
}

//This function returns the value of the private member variable balance.
double BankAccount::getBalance()
{
	return balance;
}

//This function adds the amount passed in to the balance.
void BankAccount::deposit(double amount)
{
	balance = balance + amount;
}

//this function subtracts the amount passed in from the balance.
void BankAccount::withdraw(double amount)
{
	balance = balance - amount; 
}

// Displays the data of a bank account
void BankAccount::displayData()
{
	cout << "   Account Information   " << endl;
	cout << "-------------------------" << endl;
	cout << "         Number:" << setw(15) << acctNumber << endl; 
	cout << "           Name:" << setw(15) << name << endl;
	cout << fixed << showpoint << setprecision(2);
	cout << "        Balance:" << setw(15) << balance << 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
/*****************CHECKINGACCOUNT.h*****************/

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class CheckingAccount : public BankAccount
{

private:
	double transactionFee;
public:
	CheckingAccount();   //constructor.  Initialize acctNumber to 0, name to “Unknown”, balance to 0.0, transactionFee to 1.00.
	void setTransactionFee(double trFee);  // sets instance variable transactionFee
	double getTransactionFee();    //returns the value of instance variable transactionFee
	void deposit(double amount);   //adds the amount  to the balance and subtracts  the transactionFee from the balance
	void withdraw(double amount); //subtracts the amount  from the balance and subtracts  the transactionFee from the balance
	void displayData( );   
};

CheckingAccount::CheckingAccount():BankAccount()
{  transactionFee = 1.00;  }

void CheckingAccount::setTransactionFee(double trFee)
{  transactionFee = trFee; }

double CheckingAccount::getTransactionFee()
{  return transactionFee;  }

void CheckingAccount::deposit(double amount)
{ (balance += amount) - transactionFee; }

void CheckingAccount::withdraw(double amount)
{ (balance -= amount) - transactionFee; }

void CheckingAccount::displayData()
{
	BankAccount::displayData(); 

	cout << fixed << showpoint << setprecision(2);
	cout << "Transaction Fee:" << setw(15) << transactionFee << endl;
}	
Well, for starters, you don't seem to be calling "setTransactionFee" or "getTransactionFee" anywhere in the code you've posted. How can you tell whether they're doing the right thing?
@MikeyBoy My professor doesnt want us to change the cpp or bank account header at all. just wants us to create a header file for checking account.
Well, what makes you think those methods "will not do its job correctly"? What is it that you want them to do, and how are they failing?
Topic archived. No new replies allowed.