Ok so im making a small game and i am unsure weather to put the function prototypes in the player class or the bank class, i also will have another class so what do i do? Do i make a base class and put the function in there then have the other classes inherit from it? but then what do i put in the base class? Im making a banking game where you manage peoples money so i have a player class and a bank class so far and nothing in main. I dont really know of anything that the two classes could inherit from the base class so it seems kind of pointless to make a class that just holds function prototypes.
It is possible that maybe you are misunderstanding the use of function prototypes. I am not assuming this but 99.9% of the time we put the function prototypes in a .h(header) file with the same class name. You would not make a whole new class just for prototypes.
for instance:
1 2 3
//doesstuff.h
void doStuff(); //prototype
Then...
1 2 3 4 5 6 7
//doesstuff.cpp
void doStuff()
{
//here this function does stuff //function define.
}
Most programmers put them in their own seperate .h file but name it with the same as the .cpp.
Then they include the .h file in the .cpp along with the function declarations.
im not understanding you. So they put function prototypes in their own .h file, i understand that, but then they name the .h the same as the .cpp file? so i would name it main.h?
well main does not ever use .h files because it is special. Sorry I am confusing you! Ha, I was trying to do the opposite.
I am referring to when you break up your program into multiple .cpp files. Usually you will put your function prototypes in a .h file and then the class/function definitions in a .cpp file with the same name so you do not get confused on where to find what.
thats quite a long article i only read the first post but i'll read more. I thought classes only go in .h files and not in .cpp files thoug? could you show me a code example? that would help me understand
oh, yeah i already do that. I think you guys were the onese who were confused by what i was asking I meant to say function prototypes in the player class or what because im going to have 3 different classes, possibly 4. so basically what im asking is where do the function prototypes go when a function will have to use variables from all 4 classes.
ya ok so function and class prototypes go in a .h file, classes go in a .h file, and function definitions go in a .cpp file correct? also what was extern? what does that do? also i thought you put function prototypes in the class?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class CLASS
{
public:
public();
~public();
void function();
void function2();
private:
int variable;
};
main.cpp
void CLASS::function
{
variable;
}
because how else can you do CLASS::function() if its not in the class?
Hold on, are you saying the member functions of your classes take other classes as a parameter? Just include the proper header or forward declare the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//MyFunction.h
#ifndef MYFUNCTION_H
#define MYFUNCTION_H
class Player;
class Bank;
class Map;
class HackingTools;
struct funkyWrapper{
void funky(const Player&, const Bank&, Map&, const HackingTools&);
};
#endif
To answer your questions about which goes where:
-Header files usually contain just the function prototypes and/or class definitions. Specifically where they are depends on how you organize your code.
-Implementation files (.cpp) usually contain the definitions of functions.
-There are exceptions to these guidelines, like inline and template functions.
-extern declares a variable that can be used in more than one source file.
No function prototypes go in a .h file. Definitions go in source files. Extern is for using variables across multiple files in c. Don't use it as there are better ways. Methods go in a class not prototypes
ya ok so function and class prototypes go in a .h file
Whether you put a function declaration in a .h file depends on the visibility you want the function delcaration to have. If you want a function visible to any module that sources in that .h file, then yes, put the function declaration in the .h file.
However, you often see function declarations used as forward declarations within a .cpp file. If these function delcarations don't need visibility in other .cpp files, then it is perfectly acceptable to put the function declarations in a .cpp file.
also what was extern? what does that do?
extern tells the compiler a symbol (function, variable, etc) is in another module. It allows the name to be referenced without having to be defined in the referencing module. The symbol will need to be defined in one and only one .cpp file. This creates a program wide global. Globals should be avoided where possible.
i thought you put function prototypes in the class?
Not necessarily. Functions can be global (unrelated to a class) as funky is in Daleth's example.
but what about when i want to use a variable from the class? then i have to make a ton of objects in each and every single class instead of just doing CLASS::function() which prevents me from having to do that.
ok, well then why do people put function prototypes in classes then? until now i have always seen that been done. and if i put function prototypes in a .h file then i i would have to create an object in every class correct? or could i create the object in a function and just use that?
If a function is a member function, then it goes in a class. If it's an independent function (not a member), then it stays outside. I think you may be confusing what we're saying.
If you put function prototypes in header files, the only reason you'd create an object there is for a default argument:
void funky(MyClass = MyClass());
And I'm pretty sure what you're going for requires passing the objects and not creating them in the function:
1 2 3 4 5 6
class Player{
public:
//...
void doSomething(Bank&, Map&, const HackingTools&); //You are not creating objects here
//...
};