cassius Posted November 6, 2013 Share Posted November 6, 2013 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. Quote 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 More sharing options...
Rick Posted November 7, 2013 Share Posted November 7, 2013 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 } 1 Quote Link to comment Share on other sites More sharing options...
cassius Posted November 7, 2013 Author Share Posted November 7, 2013 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. Quote 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 More sharing options...
nate066 Posted November 7, 2013 Share Posted November 7, 2013 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. 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted November 7, 2013 Share Posted November 7, 2013 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. 1 Quote Link to comment Share on other sites More sharing options...
cassius Posted November 7, 2013 Author Share Posted November 7, 2013 thanks nate066. Very usefull. Quote 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 More sharing options...
xtreampb Posted November 10, 2013 Share Posted November 10, 2013 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. Quote 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 More sharing options...
cassius Posted November 10, 2013 Author Share Posted November 10, 2013 I learned the basic syntax of c\ c++ fairly quicklt. Its scope and visibility between files that has always given me a problem. Quote 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 More sharing options...
Tim Shea Posted November 22, 2013 Share Posted November 22, 2013 Another thing to keep in mind is that if you plan on reusing elements of game code across multiple games, wrapping your classes in a namespace is a very good idea. Namespaces keep your class names from polluting the global context. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.