Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

Well, I just need to learn a more simple loop then the one's here and my teacher taught me, is there a loop that simply puts you back to asking a question instead of puting it in a while doing math to make it stop?

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

The only loops I know are "while" and "for", I want to know a line of coding that makes the program go back to a certian code upon something not being inserted, like, instead of doing

 

while ((b>2) || (b<1))

{

cout <<"Enter the number of what you want to do

1=add,2=subtract)"<<endl;

 

cin >> b;

 

if ((b>2) || (b<1))

 

cout <<"Error"<<endl;

 

}

 

I would like a line of coding that makes me go back to the certain line of coding instead of all that paragraph...

Link to comment
Share on other sites

This command is evil!!! It can ruin the stracture of your program. I will only teach you this command, if you will promise me to use it carefully, only if it it will save you tons of if's, and only after you make sure it will only affact a small and limited section of your program, I will teach you that forbidden command.

Link to comment
Share on other sites

  • 3 weeks later...

Very well. Open your mind to the evil command, and do not let it corrupt your soul.

 

first, you need an identifier. The identifier's name must follow all of the variables names rules:

my_identifier: //Piece of code

 

Then, to jump to that identifier, write

goto my_identifier;

 

So your loop will look like this:

loop_start:
if(!((b>2)||(b<1))) goto loop_end;
cout <<"Enter the number of what you want to do 1=add,2=subtract)"<<endl;
cin >> b;
if ((b>2) || (b<1))
 {
  cout <<"Error"<<endl;
  goto loop_start;
 }
loop_end:

 

Be carefull.

Link to comment
Share on other sites

One of the main ideas of classes is that the user (the user is a programer him/herself, but I call him/her a user, because he/she is using the class we write) will not have to deal with the data of our class, only to call it's functions. However, when the user creates an object of our class, it can contain anything. We want a way to initilize this object, to contain usefull data.

 

Lets say we have a Point(classes names start with capital letter by tradition. It makes the program easier to read) class, containing x and y. we want that when the user creates an object of the type point, both x and y will be set to 0. we do it like that:

class Point
{
 private:
  int x,y;
 public:
  Point()
   {
    x=0;
    y=0;
   }
  /*all the other functions of the class Point.
     you can put functions before the constractor, but I didn't wanted to
     say the same thing before the constractor*/
};

You are probebly asking yourself, "What's up with that Point function, containg no return type, not even void? Is someboddy crazy"

This function is called constractor C-O-N-S-T-R-A-C-T-O-R. Or shortly c'tor. It is called when the object is created in the memory of the computer - when you declare it. It's name is the name of the class, and it has no return type - since it can't return anything.

 

The constractor is also called when you use the "new" command.

 

The constarctor can also have arguments, so the user can set the values of the object when it's created. You do it like that:

class Point
{
 private:
  int x,y;
 public:
  Point()
   {
    x=0;
    y=0;
   }
  Point(int nx,int ny)
   {
    x=nx;
    y=ny;
   }
  //all the other functions of the class Point.
};

Hey, what's going on? How come two functions have the same name?

Those who know JAVA can tell you that this is called function overloading. Many functions can share the same name, as long as they have different argument list, so the compiler will know which function you are calling. You can do it with every function, not just a constractor. This is a C++ feature, so it won't work on C.

So, how do you use a constractor with arguments? It's very simple:

Point p1;//p1.x is 0,p1.y is 0
Point p2(3,5);//p2.x is 3,p2.y is 5
Point *p3=new Point(4,2)//p3->x is 4,p3->y is 2

 

You can also use a constractor with arguments to create imidiate values of you class. Just look at the example:

Point p1;
p1=Point(7,6);

"Point(7,6)" creates a point on the run and call the constractor, which sets it's x to 7 and it's y to 6. Then that point is copied to p1, and the temporary point is destroyed.

 

C++ let you chose what to do when an object of you class is destroyed in the end of a function that the object is part of, when the program ends, when you use the "delete" command, or when you use it as an imidiate value, like in the latest example. You do it by declaring a destractor:

class Point
{
 private:
  int x,y;
 public:
  Point()
   {
    x=0;
    y=0;
   }
  Point(int nx,int ny)
   {
    x=nx;
    y=ny;
   }
  ~Point()
   {
    cout<<"Bye Bye";
   }
  //all the other functions of the class Point.
};

As you can see, the destractor is declared just like a constractor, only it has '~' before the class name. You can't declare a destractor with arguments, and ofcourse you can't declare more then one destractor for the same class.

The destractor for the class Point is pointless. However, destractors are important for classes containing pointers (Like a string class), which need to clear the memory from the data they used. It is also important for other classes like a class that goes to graphics mode on the constractor - you have to exit the graphics mode, aren't you - or a window class in windows - you need to close the window.

 

That will be all for today. Class dismissed(calling the destractor :thumbsup1: )

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