I have a header like this (header guards not shown):
class GameSystem
{
public:
GameSystem(Game *pcGame);
virtual ~GameSystem();
void Setup();
private:
void InitGame();
void RunGame();
void ExitGame();
Game *m_pcGame;
/* Properties */
int m_nWidth;
int m_nHeight;
int m_nFps;
bool m_bFullscreen;
};
Where can I define the body for InitGame()
, RunGame()
and ExitGame()
? Can I define it in my .cpp
file? If so, how? Or am I obliged to make their body in my .h
file?
I'm using Eclipse and I began typing: void GameSystem::
and then it doesn't suggest the private functions.
More Definitions of Private function Private function means any gathering of persons for the purpose of deliberation, education, instruction, entertainment, amusement, or dining that is not intended to be open to the public and for which membership or specific invitation is a prerequisite to entry.
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
In a JavaScript class, to declare something as “private,” which can be a method, property, or getter and setter, you have to prefix its name with the hash character “#”.
You can't call private member functions from main. Your public member functions can call their private functions though. Show activity on this post. I am writing a c++ program where i need to call a private member function from main function.
Yes, you can define then in a .cpp file. Just put #include "MyHeader.h"
at the beginning of the file. You'll also need to start each function like so
void GameSystem::Init(){
//stuff
}
Generally you would define both public and private functions in the .cpp
file.
One reason to define functions in the .h
file is if you want them to be inlineable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With