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
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.
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.
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:
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?