Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

One important thing about constructors I forgot to tell:

 

If you don't declare any constructor, C++ will declare a default constructor for you. The default ocnsructor has no argument's, and it does nothing.

 

If you declare a consructor with arguments, C++ will not declare the default constructor, so unless you declare an argumentless consructor yourself, you will have to use arguments every time you create an object of that class. therefor, this is illegal:

class A
{
 public:
  A(int a)
   {
    /bla bla bla
   }
};

void main()
{
 A a;
}

 

However, you can do this:

A *a;

 

Because no object is created. Ofcourse you will have to insert the argument when you use the "new" command.

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

The variables we declared untill now in the classes belong to the object. That means, that if we have variable 'a' in the class 'A', each object of the type 'A' have an 'a' variable of it's own.

However, we can declare variables, and functions, which belong to the class. These are called 'static'. So if we have a static variable 'b' in the class 'A', every object of that class will have the same variable, so if 'c' and 'd' are objects of the class 'A', and we write "c.b=3", 'd.b' will allso be equal to 3. however, if we write "c.a=7", 'd.a' will not be equal to 7, unless it was 7 before.

 

Static functions are functions belong to the class, not the object. Static functions can only access static variables, and call other static functions, unless ofcourse they get an objcet as an argument, use global object, or you declare an object inside the function. They cann't access the unstatic variables if call the unstatic functions of the object they belong to, because they belong to no object. Why would you want to declare a function with such limitations? You will see.

 

So how do you do it? You put the keyword "static" before the type of the variable, or the return type of the function, like this:

class A
{
 private:
  static int count=0;
  //bla bla bla
 public:
  //bla bla bla
  A()
   {
    count++;
    //bla bla bla
   }
  ~A()
   {
    count--;
    //bla bla bla
   }
  static int howMuch()
   {
    return count;
   }
  //bla bla bla
};

 

Do not initilize a static variable in the consturctor, because you don't want then to be initilized every time a new object is created. Instend, initilize them when you declare them.

'count' is set to 0 when the program begins. Every time a new 'A' object is created, the constructor is calles and 'count' is increased by one. Every time an 'A' object is destroyed, the destructor is called, and 'count' is decreased by one. Therefor, 'count' is the number of objects of the class 'A' currently exist.

You can use static variables and functions like you use regular class functions and variable:

A a,b;
cout<<a.howMuch();

however, you don't always have a class of that type, and it's a waste of computing time and memory to create a new instance of that class just to call the function, and in our case, substruct 1 from the result, because we don't want to count the class we just created.

Then what can we do? We can use the syntex "classname::variablename" for variables, or "classname::functionname(argumentlist)" for functions, like this:

cout<<A::howMuch();

 

Nice and simple.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Hello, I wasn't sure where exactly to post this, but I found it at a site I was browsing a while ago...I'm hoping at some point to make a simple game from this program:

 

Game Maker's Official Website

 

And also....I know squat about C++ and was wondering if anyone knew anything about a game called Session Stream?

 

I found out about it here.

 

Session Stream Official Website (Japanese)

 

Last, but not least, does anyone know where I can get some sort of Sprite-drawing Tutorial, I like to draw, but I definitely stink at drawing pixel-sprites. At some point I'd like to make a Shmup game, but that's a ways off.

 

Thanks for any and all help (especially on that Guitar Sim Session Stream!) :banghead:

Edited by Blade
Link to comment
Share on other sites

  • 1 month later...

Operator overloading is one of my favorite things in C++. JAVA doesn't have it, and therefore JAVA sucks. well, JAVA isn't realy sucks, but it's a shame it doesn't have operator overloading.

 

So what is operator overloading? lets take this 'Point' class:

class Point
{
private:
 int x,y;
public:
 void setx(int nx)
  {
   x=nx;
  }
void sety(int ny)
  {
   y=ny;
  }
 Point()
  {
   x=0;
   y=0;
  }
 Point(int nx,int ny)
  {
   x=nx;
   y=ny;
  }
 int getx()
  {
   return x;
  }
 int gety()
  {
   return y;
  }
};

 

Wouldn't it be nice that if we have:

Point a(1,3),b(5,2);

we could write "a+b" and get the point (6,5)?

Well, we can do it with C++, using operator overloading. There are two ways of doing it.

 

The first way is to use a global function:

Point operator+(Point p1,Point p2)
{
 return Point(p1.getx()+p2.getx(),p1.gety()+p2.gety());
}

 

When we name a function "operator#" when # can be any C\C++ operator exept.,->,new,delete,sizeof and (VariableType). VariableType can be reaplaced with float, char, or any other type.

 

The second way is to declare the operator function inside a class. In that way, you only need one argument. The first operand will be the object itself:

 

class Point
{
private:
 int x,y;
public:
 void setx(int nx)
  {
   x=nx;
  }
void sety(int ny)
  {
   y=ny;
  }
 Point()
  {
   x=0;
   y=0;
  }
 Point(int nx,int ny)
  {
   x=nx;
   y=ny;
  }
 int getx()
  {
   return x;
  }
 int gety()
  {
   return y;
  }

 Point operator+(Point p)
  {
   return Point(x+p.x,y+p.y)
  }
};

 

Here, we don't use getx and gety, because a function inside a class can access the private members of any class of that kind, not just of the object "this".

 

You can also overload onary operators - operators that recive only one operand, like "x++" or "-d". So how do you do it?

Overloading "+x","-x","++x" and "--x" is very simple: if you do it as a class function, you need to send no arguments to the operator function:

Point operator-()
{
 return Point(-x,-y);
}

 

Overloading "x++" and "x--" is a litle more complax. You need to add a dummy int argument to the function, that will act like the second operand - which doesn'y exist, so it's blank. Lets say that for the point p, "p++" adds 1 to both x and y:

Point operator++(int)
{
 Point tmp=this;
 ++x;
 ++y;
 return tmp;
}

Note two things:

1. We stored the current Point in tmp, because the "x++" operator returns the value of x before the addding action.

2. The operator = is automaticly overloaded for any user declared class. It simply copies all of one objects data to another. You can overload this operator if you want, for example, if your class uses pointers:

class String
{
 private:
  char str[20];
 public:
  String(int length)
   {
   }
  void operator=(String s)
   {
    for(int i=0;i<20;i++)
      str[i]=s.str[i];
   }
};

This String class is just an example. We will write a better one later.

 

Before we go on, I will need to explain a very usefull thing about functions:

If you put '&' before an argument, it will be send by referance, meaning the function will

 

be able to actualy change it's value:

#include<iostream.h>
void valswap(int a,int b)
{
 int tmp=a;
 a=b;
 b=tmp;
}
void refswap(int &a,int &b)
{
 int tmp=a;
 a=b;
 b=tmp;
}
void main()
{
 int a=4,b=6;
 valswap(a,b);
 cout<<"swapping by value: a="<<a<<",b="<<b;
 refswap(a,b);
 cout<<"\nswapping by referance: a="<<a<<",b="<<b;
}

The output will be:

swapping by value: a=4,b=6

swapping by referance: a=6,b=4

 

 

you can also put the '&' before the function name, so the function will refer to an actual variable, and you will be able to change that variable:

void &max(int &a,int &b)
{
 if(a>b)
   return a;
 return b;
}

Now you can write "max(x,y)=8", and the bigger between the two variables will be set to 8.

 

Now lets go on to the index operator. Lets overload an index operator to our String class:

class String
{
 private:
  char str[20];
 public:
  String(int length)
   {
   }
  void operator=(String s)
   {
    for(int i=0;i<20;i++)
      str[i]=s.str[i];
   }
  char &operator[](int loc)
   {
    return str[loc];
   }
};

The argument (the second argument when used outside of a class), is the number between '[' and ']'. Now we can access the variables inside our String objects just like a normal string - a char array.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

  • 3 weeks later...

Hey somebody , for the first time since december im confused in C++ class. Im having trouble understanding substring and how to use it instead of using char's.

 

Edit: I've tried gamemaker out before, pretty simple

Edited by Drake
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...