linker errors in inheritance exercise

i'm doing an exercise from the deitel c++ book. i think i have the implementation figured out, but i'm having some errors when i try to compile it. i'm getting "[linker errror] undefined reference to" for each of the SavingsAccount and CheckingAccount class constructors as well as one for every function in the account class. i've checked to make sure all the #include's are there and all the files are in the same folder but no luck. anyways, here's my code (new here, sorry if the formatting is off.)

Driver Program (provided to me)
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
#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();
   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; 
   system("pause");
   return 0;
}; // end main 


Account header and .cpp file
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
#ifndef Account_h
#define Account_h
class Account
{
 public:
        Account(double);
        virtual bool debit(double);
        virtual void credit(double);
        double getBalance();
        void setBalance(double);
 private:
         double balance;
};
#endif

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

Account::Account(double start)
{
 if ( start >= 0)
    balance = start;
 else
     {
      balance = 0;
      cout << "initial balance must be non-zero and positive" << endl;
     }
}

bool Account::debit(double subtr)
{
 if (balance >= subtr)
 {
    balance = balance - subtr;
    return true;
 }
 else
 {
    cout << "debit ammount is greater than balance" << endl;
    return false;
 }
}

void Account::credit(double cred )
{
     balance = balance + cred;
}

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

void Account::setBalance(double bal)
{
 if (bal >= 0)
    balance = bal;
}


SavingsAccount header and .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Account.h"

class SavingsAccount : public Account 
{
 public:
        SavingsAccount(double, double);
        double calculateInterest();
 private:
         double interest;
};

#include "SavingsAccount.h"
 
SavingsAccount::SavingsAccount( double bal, double inter)
: Account(bal)
{
 interest = inter;
}

double SavingsAccount::calculateInterest()
{
 return interest * getBalance();
}


Checking account header and .cpp file
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
#include "Account.h"

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

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount(double bal, double AccFee)
: Account(bal)
{
 fee = AccFee;
}

void CheckingAccount::credit(double cred)
{
 Account::credit(cred);
 Account::setBalance(getBalance() - fee);
}

bool CheckingAccount::debit(double deb)
{
 if ( Account::debit(deb) == true)
    {
    Account::setBalance(getBalance() - fee);
    return true;
    }
 else
     return false;
}
Make sure you're compiling everything correctly. If you're using an IDE, for example Code::Blocks, Dev-C++, MSVC++, etc., creating a project and adding all .h and .cpp files should properly compile this. If you're compiling by hand, using the command line, you will first need to compile each class, .h and .cpp, and then compile the outputs of them with main.cpp.

Note: It compiles perfectly fine on my machine.
I'm using dev-c++ and putting it all into a project worked, thanks so much. I never had to use a project file before since it all compiled just fine without it (though it was never more than one header file). is there specific requirements for when they must be used?
General rule of thumb, if there is more than one .cpp file, the projects will help you compile accurately. However, I do not recommend using dev-C++ and highly suggest getting rid of it. It has a simple design, but has many flaws. You should always use a project so that all files related to that program are only a click away and so that it keeps everything organized a little better.

There are some exceptions of the general rule, including templates, but you'll learn over time.

If you're wondering, I suggest Code::Blocks. A new version was just release two weeks ago: http://www.codeblocks.org/downloads/26. Make sure you download the mingw-setup version or you won't be able to compile any code (unless you know what you're doing).
Topic archived. No new replies allowed.