Jump to content

classes in c++


cassius
 Share

Recommended Posts

I am trying to learn classes in c++ and have a few questions..Does each class need its own header file or can I put several class definitions in one header.

 

What scope does a class have? is it global or not.

 

If I need a separate header file for each class where do I put my gameplay code ?

Hope this is clear and someone can clarity the problem.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

You can put multiple class definitions in a file but it's very common practice to use 1 header/source file per class. I would stick with that if I were you.

 

A class doesn't really have scope because it's a blueprint of a type. When you create an object of a given class the scoping of that object follows the same rules as any other variable, which means you can make the variable global if you want (I would avoid this however).

 

Look at a C++ project in Leadwerks 3. Notice how it has an App class which an object of the App class gets created in int main() inside Main.cpp. The scope of that app object created from the App class is local to the int main() function. Then we call app->Start() to init some stuff then app->Loop()? (Update maybe? can't remember the name), and this is where the game runs. Now as far as structuring your game with classes this is a really big topic in which people have strong opinions about based on efficiency and maintainability of the project.

 

Normally you will want to put your class declarations in the header file like:

 

class Foo
{
private:
int i;
public:
void Update();
};

 

Then in the source file of the same name as the header file just different extension

 

void Foo::Update()
{
// actually do something
}

 

 

So classes just group variables and functions into a common object. Think of real world objects like a stapler. It has variables like color, size, paperClip count. It has functions like Staple(), Reload(), etc. You can represent this in plain C but nothing really links them together in C. In C++ the class is a way to link the variables and classes together in a way that makes sense.

 

 

In a LE3 C++ project in main.cpp.

 

App* app = new App;        // create a variable of the App blueprint

if (app->Start())                // call this classes Start function
{
  while (app->Loop()) {} // call this classes Loop() function
  delete app;                  // delete the app variable
}

  • Upvote 1
Link to comment
Share on other sites

Thanks rick that thrown more light on the subject. Looks like I have to undo a lot of stuff in my game in order to implement classes but in the long run it will be worth it.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

Thanks rick that thrown more light on the subject. Looks like I have to undo a lot of stuff in my game in order to implement classes but in the long run it will be worth it.

If you still need help with c++ classes check out these tutorials. There all in a playlist, this specific tutorial talks about putting classes in separate files.
  • Upvote 1
Link to comment
Share on other sites

Following tutorials is a good idea. The first thing that stands out is the #ifndef stuff at the top. I think that's pretty old school these says where you can just use #pragma once instead, which is cleaner and I think most all modern compilers support it.

  • Upvote 1
Link to comment
Share on other sites

but i think people should be aware of the #ifndef because it help teaches preprocessor logic and methods. Not too much different but helps ease the exposure.

bool Life()
{
 while(death=false)
 {
   if(death==true)
   return death;
 }
}

 

I have found the secret to infinite life

 

Did I help you out? Like my post!

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...