Jump to content

Recommended Posts

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

Posted (edited)

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
  • 1 month later...
Posted

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.

  • 3 weeks later...
Posted (edited)

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
Posted

You don't use substrings instead of chars. You still use chars if you want a single char. Substrings represent parts of strings which can have more then a single char.

Posted

Char's are much easier for me then strings, and take up less memory, but my teacher was forcing us to use only strings to find single letters.

 

string a;

string b;

string c;

 

cout <<"What word are you using?"<<endl;

cin >> a;

cout <<"Wich letter would you like to replace?"<<endl;

cin >. b;

cout <<"Wich letter would you like to replace it with?"<<endl;

cin >> c;

 

//Now there's a for loop and whatnot then you have to check it using substr because you can use a[1]==b. Wich I got today, but now there's a even harder thing in class but I'll get it eventually

Posted

Well, I studied the substring a little more, and it seems that there is a major diffreance between SubString and char. SubString represents a part of an actual String, so if you change the SubString, the String will change as well.

 

If you want to convert a char to a SubString, write this simple class:

 

class CharMadeSubString:public SubString
{
 private:
  String sourceString;
 public:
  CharMadeSubString(char ch)
   {
    sourceString=ch;
    SubString(sourceString,0,1);
   }
};

 

You can put it in an header file if you know how. I didn't tested the code, so if there is a problem, just tell me. It will not act like a normal SubString, but you will be able to use it with functions require SubString.

If you don't understand some of the code, just write it, and use it like a function, like "CharMadeSubString('a')". I will probebly get to it later on, and so will your teacher.

Posted

Here are some speciel operators:

 

The () operator. This operator is declared inside a class like that:

return_type operator ()(arguments)

 

and you use it like this:

object(arguments)

 

The speciel thing about this operator, is that it can have as many arguments as you want.

 

 

The casting operator is used to change the type of the object, declared inside a class like this:

operator type()

 

The speciel thing about this operator, is that you don't need to write the return type before the "operator" keyword. The compiler knows what the return type is according to the operators name.

 

It is called when you try to put the object inside a variable of a different type, or when you call it directly: (type)object);

 

class Cls
{
 private:
  int value;
 public:
  Cls(int v)
   {
    value=v;
   }
  operator int()
   {
    return value;
   }
};

void main()
{
 int integer=Cls(9);
}

Now integer is set to 9. Ofcourse you can put more complex code inside the operator function.

 

 

The intput/output operators, << and >>, are acting just like the normal operators, but they are the most complex ones. That is because the way you use them. As you remember, with a member(inside a class) operator function, the first operand is the object itself, stored in the variable "this". Lets take a look at this simple printing command:

cout<<o;

The first operand is cout!!! That means that if o is an object, the operator function can not be a member of o, and it can not use it's private memeber variables, which we want to print!!!

So how do we solve the problem? There are three ways:

1)We can supply our class with get and set functions. However, that way, the whole world can have access to it's private members, and we don't want that.

2)We can make the operator function a friend of our class. However, if I will teach you that, and my programing teacher will find it our, she will kill me. I will be killed anyways, since I tought here the goto command, and I don't think I can be killed twice. So this is not an option here.

3)We can supply our class with read/write functions. This will be what we do:

 

#include<iostream.h>
class Cls
{
 private:
  int value;
 public:
  Cls(int v)
   {
    value=v;
   }
  void write(ostream& ostrm)
   {
    ostrm<<value;
   }
};

ostream& operator<<(ostream& ostrm,Cls cls)
{
 cls.write(ostream);
 return ostream;
}

void main()
{
 cout<<Cls(5);
}

You can do the same thing with the input operator >>.

 

The reason the operator function returns ostream& is that it will be possible to chain:

cout<<"Cls="<<Cls(5)<<"got that?\n";

 

 

That will be all for today. Class dissmissed.

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...