0

I'm currently learning SDL for creating graphical applications/games in C++ and have a few questions about how you create and use voids within classes.

For my current project I'm creating a very basic pong game and would like to create a class that not only contains information about an object's position, loaded bitmap and other variables but also a function that covers the AI/Movement Engine and another for the rendering.

I would like to code it so that I could format my code like this: (All code below is pseudo code and only represents the structure of what I want to achieve)

class Object
{
    private int stuff;
    private void manageStuff();
}

void Object::manageStuff()
{
    stuff++;
}

Object object = new Object();

object.stuff = 0;

for (int i = 0; i < 10, i++)
{
    object.manageStuff();
    cout << object.stuff;
}

Are there any obvious ways of doing this in C++?

Flexo
  • 87,323
  • 22
  • 191
  • 272
TotalJargon
  • 147
  • 2
  • 2
  • 14
  • There are a number of problems with that code, but it's all "close" to correct C++. Have you tried compiling a program? Are you stuck or confused by anything specific? – aschepler Jul 29 '12 at 15:50
  • Not really sure what the question is really, seems like basic C++ to me. You have a few syntax errors that make it seem like you are more used to Java. Are you asking for help to fix the syntax errors? Or is a more general design question? – jahhaj Jul 29 '12 at 15:51
  • It's all pseudo-code, and also I'm surprised that it looks like Java seeing as I have never programmed in it :) the main bit I'm confused with is the syntax of creating a void that will only change the variable of it's constructor class even though the variable in the void will always be the same, so if there are 2 of Object, then running the method on object1 wont affect the variable in object2 despite the variable name in the void being hard-coded – TotalJargon Jul 29 '12 at 15:54
  • You mention "use voids". Are you talking about having a void return type for a method? – Vaughn Cato Jul 29 '12 at 15:55
  • 1
    I think you're confusing the return type of a function/method with it's language name. Don't say voids like that say member function or method. – Flexo Jul 29 '12 at 15:59
  • @TotalJargon You don't need to worry, your 'void' will behave exactly as you want it to. – jahhaj Jul 29 '12 at 16:02
  • C# is very similar to Java which is probably why @jahhaj made that observation. I strongly suggest you sit down with a C++ textbook if this is the language you wish to use for this project. Your terminology isn't accurate in your question. It will also help you post more correct code. – Code-Apprentice Jul 29 '12 at 21:22

2 Answers2

0

I think you're looking for constructors and initalizer lists. There are a number of other issues in the example you showed too, which I've fixed and commented in place. Perhaps:

#include <iostream>

class Object
{
// Note: access modifier written like this:
public:
    int stuff;
    void manageStuff();
    Object(); // Constructor which takes no arguments
}; // note semicolon

void Object::manageStuff()
{
    stuff++;
}

Object::Object() : stuff(0) {
  // sets stuff to 0 upon initalization.  
  // could also have written:
  // stuff=0;
  // but in this case initalization is nicer.
}

Object object; // no need for new here

// initial value of stuff set via constructor now,
// you can't just write random statements anywhere

int main() {
  for (int i = 0; i < 10; i++) // note semicolon not comma
  {
      // if these were private this would fail:
      object.manageStuff();
      std::cout << object.stuff << "\n"; 
  }
}

Is more like what you were looking for.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • That's great thank you! works just as I needed ^^ now if I alter that code to have object1 and object2 and have manageStuff() work independantly! :D (not sure if your IDE/Comiler allows but you can add 'using namespace::std' to remove the need for std:: in front of many functions like cout) – TotalJargon Jul 29 '12 at 16:06
  • @TotalJargon I use emacs and I can type whatever I like, but I usually [deliberately avoid `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c) – Flexo Jul 29 '12 at 16:08
0

No. If you wish to use C++, you will have to actually learn C++. This involves, at least, learning C++'s syntax. Trying to force it to emulate the syntax of another language is not going to be possible or beneficial.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Sorry about the badly written code, was slightly rushed so I missed silly things like new being used in VC# not C++ – TotalJargon Jul 29 '12 at 16:11