C++ Inheritance Code - Error problems...

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
// Lab 1: bankAccounts.cpp
// Test program for Account hierarchy.
#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setprecision;
using std::fixed;

#include "Account.h" // Account class definition
#include "SavingsAccount.h" // SavingsAccount class definition
#include "CheckingAccount.h" // CheckingAccount class definition

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 << "\nAttempting to debit $25.00 from account1." << endl;
   account1.debit( 25.0 ); // try to debit $25.00 from account1
   cout << "\nAttempting to debit $30.00 from account2." << endl;
   account2.debit( 30.0 ); // try to debit $30.00 from account2
   cout << "\nAttempting to debit $40.00 from account3." << endl;
   account3.debit( 40.0 ); // try to debit $40.00 from account3

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

   cout << "\nCrediting $40.00 to account1." << endl;
   account1.credit( 40.0 ); // credit $40.00 to account1
   cout << "\nCrediting $65.00 to account2." << endl;
   account2.credit( 65.0 ); // credit $65.00 to account2
   cout << "\nCrediting $20.00 to account3." << endl;
   account3.credit( 20.0 ); // credit $20.00 to account3

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

   // add interest to SavingsAccount object account2
   double interestEarned = .10;
   /* Declare a variable interestEarned and assign it the interest
      account2 should earn */
   cout << "\nAdding $" << interestEarned << " interest to account2." 
      << endl;
   account2.calculateInterest( account2.getBalance() , interestEarned);
   /* Write a statement to credit the interest to account2's balance */

   cout << "\nNew account2 balance: $" << account2.getBalance() << endl;
   return 0;
}


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


#ifndef SAVINGS_H
#define SAVINGS_H

#include "Account.h"

class SavingsAccount : public Account

{
public:
	SavingsAccount(double, double);
	double calculateInterest(double, double);
private:
	double interestRate;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
// SavingsAccount.cpp



#include "SavingsAccount.h"
SavingsAccount::SavingsAccount(double initialBalance, double interestRate)
	:Account(interestRate)
double SavingsAccount::calculateInterest(double balance, double interestRate)
{
	return balance*interestRate;
}


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


#ifndef CHECKING_H
#define CHECKING_H

#include "Account.h"

class CheckingAccount : public Account
{
public:
 	CheckingAccount(double, double);
	void credit( double );
	bool debit( double );
private:
	double transactionFee;
	void chargeFee();
};

#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
// CheckingAccount.cpp

#include <iostream>
using std::cout;
using std::endl;

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount(double initialBalance, double transactionFee)
	:Account(transactionFee)
void credit(double money)
{
	
	Account::setBalance(Account::getBalance() + money);
	chargeFee();

}
void debit(double money)
{
	if (Account::debit(money))
		Account::setBalance(Account::getBalence() - money);
		chargeFee();

	else break;
}
void chargeFee()
{
	Account::setBalance(Account::getBalance - transactionFee);
	cout<<"\n"<<" Charged a fee of "<<transactionFee;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Lab 1: Account.h
// Definition of Account class.
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
public:
   Account( double ); // constructor initializes balance
   void credit( double ); // add an amount to the account balance
   bool debit( double ); // subtract an amount from the account balance
   void setBalance( double ); // sets the account balance
   double getBalance(); // return the account balance
private:
   double balance; // data member that stores the balance
}; // end class Account

#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
// Lab 1: Account.cpp
// Member-function definitions for class Account.
#include <iostream>
using std::cout;
using std::endl;

#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 )
      balance = initialBalance;
   else // otherwise, output message and set balance to 0.0
   {
      cout << "Error: Initial balance cannot be negative." << endl;
      balance = 0.0;
   } // end if...else
} // end Account constructor

// credit (add) an amount to the account balance
void Account::credit( double amount )
{
   balance = balance + amount; // add amount to balance
} // end function credit

// debit (subtract) an amount from the account balance
// return bool indicating whether money was debited
bool Account::debit( double amount )
{
   if ( amount > balance ) // debit amount exceeds balance
   {
      cout << "Debit amount exceeded account balance." << endl;
      return false;
   } // end if
   else // debit amount does not exceed balance
   {
      balance = balance - amount;
      return true;
   } // end else
} // end function debit

// set the account balance
void Account::setBalance( double newBalance )
{
   balance = newBalance;
} // end function setBalance

// return the account balance
double Account::getBalance()
{
   return balance;
}



Ok
1] Sorry for the very long code...
2] I found this website on goodle, the tutorials are great and im trying out the forums because i have a problem
3] The problem. I just finished this code for a hw assigment [inheritance] and i am getting stuck b/c on the derived class definitions files, i get errors [at bottom] such as reached end of file unexpected error and something w/ local function definitions. anyways i got stuck because im not sure what to do. ive read the chapter over again, and i feel im missing either something very obvious and stupid or something complicated... but anyways, any help is greatly appreciated!!

Thanks for reading :]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
1>Compiling...
1>account.cpp
1>bankAccounts.cpp
1>CheckingAccount.cpp
1>c:\documents and settings\alex\desktop\chapter12\chapter12\checkingaccount.cpp(13) : error C2612: trailing 'type' illegal in base/member initializer list
1>c:\documents and settings\alex\desktop\chapter12\chapter12\checkingaccount.cpp(14) : error C2601: 'credit' : local function definitions are illegal
1>c:\documents and settings\alex\desktop\chapter12\chapter12\checkingaccount.cpp(21) : error C2601: 'debit' : local function definitions are illegal
1>c:\documents and settings\alex\desktop\chapter12\chapter12\checkingaccount.cpp(29) : error C2601: 'chargeFee' : local function definitions are illegal
1>c:\documents and settings\alex\desktop\chapter12\chapter12\checkingaccount.cpp(33) : fatal error C1004: unexpected end-of-file found
1>SavingsAccount.cpp
1>c:\documents and settings\alex\desktop\chapter12\chapter12\savingsaccount.cpp(8) : error C2612: trailing 'type' illegal in base/member initializer list
1>c:\documents and settings\alex\desktop\chapter12\chapter12\savingsaccount.cpp(9) : error C2601: 'SavingsAccount::calculateInterest' : local function definitions are illegal
1>c:\documents and settings\alex\desktop\chapter12\chapter12\savingsaccount.cpp(12) : fatal error C1004: unexpected end-of-file found
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\alex\Desktop\chapter12\chapter12\Debug\BuildLog.htm"
1>chapter12 - 8 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Neither of your CheckingAccount and SavingsAccount constructors have bodies. You need at least to put {} there.

The functions in CheckingAccount.cpp are supposed to be members of the class, but they are not written that way (you are missing CheckingAccount::)
Thanks so much!! ... i knew it would be something stupid like that. But anyways thanks alot :]
Topic archived. No new replies allowed.