What should i do here?

Pages: 12
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. 
  
}
Last edited on
wait what? so function prototypes go in a .h file of their own? or they go in the same .h file as the class that uses them?
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.
Last edited on
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.

I found you an article that may help

http://www.cplusplus.com/forum/articles/10627/
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
Last edited on
You usually define your class in the header file. I think erock was saying that you define your member functions in the cpp files.

Edit:
Saw your edit.
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
//MyHeader.h
#ifndef MYHEADER_H
#define MYHEADER_H

void funky(); //Function prototype

class Boo{
   public:
      void dostuff(char = '\0');//Default argument
      Boo(char = 'a'); 
   private:
      char c;
};

extern size_t heyo; //A global variable with external linkage

#endif

//MySource.cpp
#include <iostream>

void funky(){
   std::cout << "I'm funky!" << std::endl;
}

Boo::dostuff(char ch){
   std::cout << (ch == '\0' ? c : ch) << std::endl;
}

Boo::Boo(char ch)
   : c(ch)
{}

size_t heyo(45); //Definition of variable, but remember to not use global variables if you can help it. 
Last edited on
^ is what I was trying to get across.
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.
Last edited on
I did not read your original post, sorry. :F I always assume latest posts are follow up questions.

1
2
3
4
5
6
7
8
9
10
11
12
//MyFunction.h
#ifndef MYFUNCTION_H
#define MYFUNCTION_H

#include "Player.h"
#include "Bank.h"
#include "Map.h"
#include "HackingTools.h"

void funky(Player, Bank, Map, HackingTools);

#endif 


And if you use references and pointers only, you can forward declare the classes instead:

1
2
3
4
5
6
7
8
9
10
11
12
//MyFunction.h
#ifndef MYFUNCTION_H
#define MYFUNCTION_H

class Player;
class Bank;
class Map;
class HackingTools;

void funky(const Player&, const Bank&, Map&, const HackingTools&);

#endif 


Edit:
My point is that you should just put the function in its own files.
Last edited on
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?
Last edited on
bump
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.
closed account (Dy7SLyTq)
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.
Why does one function need to access all of the private members from multiple classes?

You have two choices:
-Friend functions (breaks encapsulation, so not really recommended)
-Use the interface of the class

1
2
3
4
5
6
7
8
9
10
11
12
class A{
   public:
      A(int I) : a(I) {}
      int get_a()const
         {return a;}
   private:
      int a;
};
struct B{
   void funky(const A& a)
      {cout << a.get_a() << '\n';}
};
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?
Last edited on
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
//...
};

But I'm just postulating.
Last edited on
Pages: 12