confused about prototyping in header files

I'm trying to declare a class called account which privately holds the balance of a virtual account but has public classes which allow one to withdraw, deposit, and check the balance. I throw exceptions if the user tries to deposit a negative value or withdraw more money than they already have. So my question is, if I put my class in my file Account.cpp, how do I access it from my main function in my main.cpp file? I am told I should prototype in my headliner class but I'm what to put in the prototype compared to what would go in the Account.cpp file. Here is what the Account class looks like

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
#include "MyException5.h"
#include <string>
#include <exception>
using namespace std;

class Account
{

private:

    double balance;

public:

    Account()

    {

        balance = 0;

    }

    Account(double initialDeposit)

    {

        balance = initialDeposit;

    }

    double getBalance()

    {

        return balance;

    }

// returns new balance or -1 if error

    double deposit(double amount) throw(MyException_DepositError)

    {
        MyException_DepositError negDep;

        if (amount > 0)
        {

            balance += amount;

        }
        else
        {

            throw negDep; // code indicating error
        }
        return balance;

    }

// returns new balance or -1 if invalid amount

    double withdraw(double amount) throw(myException_WithdrawError)

    {
        myException_WithdrawError invWith;

        if((amount > balance) || (amount < 0))

            throw invWith;

        else

            balance -= amount;

        return balance;

    }

};
you would just include the account.cpp file. Then you would declare an Account object.

1
2
3
4
5
6
7
8
9
10
11

#include "Account.cpp"

int main(){

   Account AccountBook;

   return 0;

};


when you use inclusion guards, use them on the account object, so that it doesn't get re-defined. You can save yourself a lot of trouble by getting into the habit of creating inclusion guards.

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

#ifndef __ACCOUNT__
#define __ACCOUNT__


#include "MyException5.h"
#include <string>
#include <exception>
using namespace std;

class Account
{

private:

    double balance;

public:

    Account()

    {

        balance = 0;

    }

    Account(double initialDeposit)

    {

        balance = initialDeposit;

    }

    double getBalance()

    {

        return balance;

    }

// returns new balance or -1 if error

    double deposit(double amount) throw(MyException_DepositError)

    {
        MyException_DepositError negDep;

        if (amount > 0)
        {

            balance += amount;

        }
        else
        {

            throw negDep; // code indicating error
        }
        return balance;

    }

// returns new balance or -1 if invalid amount

    double withdraw(double amount) throw(myException_WithdrawError)

    {
        myException_WithdrawError invWith;

        if((amount > balance) || (amount < 0))

            throw invWith;

        else

            balance -= amount;

        return balance;

    }

};

#endif 


Notice that the inclusion guard surround the entire object. Essentially what this is saying is that if this symbol (in this case __ACCOUNT__) has not been defined, define it and everything else after it until you see a terminator flag.

This is good practice.
I've been told never to #include a .cpp file. Is there another way I can access it through my main?
What they were saying is never include the definition file. But you have essentially unified the two into one whole file. Thats why i said to just include .cpp.

All the declarations are in one file, the definitions in the next.

in Thing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef __THING__
#define __THING__

class Thing
{
public:

   Thing();
   void DrawThing();

protected:

   int Thingy;
};

#endif 


then, in Thing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13

#include "Thing.h"

//constructor with the initializer list
Thing::Thing():
Thingy(0)
{};

//function Draw thing takes nothing and gives nothing back!
void Thing::DrawThing()
{
//draw the thing
};


notice the syntax: for functions that return a value, the order is:

[type] [class identifier]::[function name](argument list)

I this case you would NOT include the .cpp file, because the .cpp file is the implementation, and not the declaration.

Then you would do the same in the main file.
1
2
3
4
5
6
7
8
9
10

#include "Thing.h"

int main()
{
   Thing Adams;           //create a thing object
   Adams.DrawThing();//call the DrawThing() Method

  return 0;
};
Last edited on
This makes sense, but again looking back to my example, I'm confused what exactly do put in my header file compared to my .cpp file. In my header file do I just do the functions with no body or returns?

something like this in my header?

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
#ifndef MYEXCEPTION5_H_INCLUDED
#define MYEXCEPTION5_H_INCLUDED
#include <stdexcept>
#include <iostream>
#include <string>
#include <exception>

using namespace std;

class myException_WithdrawError: public exception
{
      public:
              virtual const char* what() const throw()
              {
                return "Caught a withdraw error! You can't withdraw more than your balance.";
              }

};

class MyException_DepositError: public exception
{
      public:
              virtual const char* what() const throw()
              {
                return "Caught a deposit error exception! You can't deposit a negative amount";
              }

};

class MyException_BalanceError: public exception
{
      public:
              virtual const char* what() const throw()
              {
                return "Caught a balance error exception! You can't have a negative amount";
              }

};
class Account
{
private:

    double balance;

public:

    Account();
    Account(double initialDeposit);
    double getBalance();
    double deposit(double amount);
    double withdraw(double amount);


};

#endif // MYEXCEPTION5_H_INCLUDED 
Last edited on
figured it out, thanks for helping pogrady
Topic archived. No new replies allowed.