Define sections?

Started by
7 comments, last by Xsis 17 years, 12 months ago
Hey! i'm having a little question going around in my own head. I've programmed alot in Qbasic in the old days. I can remember you could use If statements and Goto. can you also use this feature in C++? like. If cin.get() then goto horse; horse: etc.etc.etc... something like that.?
I wanna become a good programmer for my game :-)
Advertisement
Yes, It is possible.
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
While goto exists in C, using it is considered very harmful (classical Dijkstra's article on the subject). You should consider better alternatives - such as proper flow control.

C and C++ defines the following flow control instructions:
if (expr) { instr; } else { instr; }for (expr1; expr2; expr3) { instr; }while (expr) { instr; }do { instr; } while (expr);switch (expr) {   case constant1:    instr;    break;  case constantN:   instr;   break;  default:   instr;}

In my examples, expr are expressions and instr are instructions.

For more information, I suggest you to find a good C/C++ tutorial.

Regards,

[edit: forgot switch!]
Wow, Wow! Easy now! can you place some comments! i'm just started at programming in C++ :-)

another thing, can't I just make a Loop that awaits for like the enter button to be pressed? before it continues?
I wanna become a good programmer for my game :-)
Hi,
You will not really need goto. Here is a link where you will find interesting observations ont goto:
http://www.cppreference.com/keywords/goto.html

In fact, there are other ways to build up a program. Let's get back at your example. You want to display a different picture and sound according to the type of animal you have.

A first solution is to have a switch statement:
#define HORSE 0#define SHEEP 1#define DUCK  2switch (AnimalType) {  case HORSE: //play horse sound and draw horse     break;  case SHEEP: //play sheep sound and draw sheep     break;  case DUCK: //play duck sound and draw duck     break;  default:     //handle unknow beast here     break;}

That is simple to use and fast to implement. But if you have 100 different animals to handle, that would lead to have an unmanageable switch statement.

A better solution here would be to use a class in C++. You define a base animal object and send into its constructor the characteristics of the animal. Then you don't need to check the animal type for drawing, you just call the display function of each instance of the animal class.

#define NBANIMALS 3#define HORSE 0#define SHEEP 1#define DUCK  2class Animal {   public:      Animal(sound* pSound, picture* pPicture):          mSound(pSound), mPicture(pPicture){};      Show(void){Play mSound and Draw mPicture};   protected:      sound* mSound;      picture* mPicture;}Animal* Farm[NBANIMALS];void InitAnimalList(void){   Farm[HORSE] = new Animal(HorseSound, HorsePic);   Farm[SHEEP] = new Animal(SheepSound, SheepPic);   Farm[DUCK] = new Animal(DuckSound, DuckPic);}void Main() {//main procedure   //We create the list   InitAnimalList();   //we display all animals   for (int i=0; i<NBANIMALS; i++)      Farm->Show();   //we destroy the animal classes   for (int i = 0; i <NBANIMALS; i++)      delete Farm;}


Under BASIC the keywords GOTO and GOSUB existed because there wasn't any functions at the beginning (it was added later in more evolved versions of the language). Under C++, you won't need it since you now have functions, classes, inheritance, overloading. C++ provides you tools to write more understandable and maintenable code. It just takes time to master as all new languages we learn.

Hope I answered your question.
Ghostly yours,
Red.
Ghostly yours,Red.
@Xsis:
Yes you can. When searching the net, you will find many code samples for that. Look after the function getch().
Ghostly yours,
Red.
Ghostly yours,Red.
Answered alot thanks :-) but some of the code in classes i don't quite understand.

class Animal {   public:      Animal(sound* pSound, picture* pPicture):          mSound(pSound), mPicture(pPicture){};      Show(void){Play mSound and Draw mPicture};   protected:      sound* mSound;      picture* mPicture;}Animal* Farm[NBANIMALS];void InitAnimalList(void){   Farm[HORSE] = new Animal(HorseSound, HorsePic);   Farm[SHEEP] = new Animal(SheepSound, SheepPic);   Farm[DUCK] = new Animal(DuckSound, DuckPic);}



why do you make it. sound*, pSound? why the star and why the p?
and mSound and mPicture?

i'm not good at understanding, but i'm trying :-)
I wanna become a good programmer for my game :-)
Ok. The example I provided you is pseudo code but I will elaborate on it since it shows a constructor.
For the animal class, I want to initiate it with a sound and a picture. So I declare the class constructor (function with the same name as the class) with two parameters:
- a pointer to a sound object (this is why you have a * character after the typename) named pSound.
- a pointer to a picture object named pPicture.

When a class is created, it calls the constructor for initiation. Here, the animal constructor stores the parameters pSound and pPicture into the animal class member variables mSound and mPicture. When the function Show is invoked it will use the member variables of the class to display and sound the animal.

Note that leading p and m characters are a naming convention. There exist many naming conventions, using one boils down to a matter of preference and readability. If you like, you can prefix parameter variables with p and member variables with m: this helps distinguishing from where the variable comes from in a function.

I suggest you to read a few tutorials to learn more about C++. Here are a couple online:
http://www.cprogramming.com/tutorial.html
http://www.relisoft.com/cpp.html (read previous tutorials before this one)

Hope that helps.
Ghostly yours,
Red.
Ghostly yours,Red.
It Did!
Thanks Red! :-D

i'm at lesson 12 now at www.cprogramming.com :-D
I wanna become a good programmer for my game :-)

This topic is closed to new replies.

Advertisement