Advertisement

C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by June 01, 2006 11:40 AM
182 comments, last by Dbproguy 16 years, 4 months ago
For the tutors: maybe you might wanna make a Tutors PM box and have the "students" post their assignments or extra credit there. i say this only because one posts and everyone copies (well not everyone but enough).

anyway, just a suggestion.

Beginner in Game Development?  Read here. And read here.

 

Quote: Original post by twoaterisn
is iostream part of the standard C++ library?


Yes.

Quote: is namespace std part of iostream?
is namespace std part of the standard C++ library?


The other way round: all the components of the standard C++ library have been placed in the std namespace. It is the way the library is organized.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote: Original post by Alpha_ProgDes
For the tutors: maybe you might wanna make a Tutors PM box and have the "students" post their assignments or extra credit there. i say this only because one posts and everyone copies (well not everyone but enough).

The problem is simple enough that there really shouldn't be a lot of variability in the results. Plus, trying to police cheating is doubly unprofitable: participation is voluntary, sharing is encouraged (I believe; I'm not a tutor), and participants should be motivated by their own desire to learn - which is not served by blindly copying others.

It's really more work than it's worth, creating a "drop box" functionality for the participants.
Greetings All,

Namespaces are a not really a complicated topic, but without a more in-depth knowledge of C++ than chapters 1 and 2, it can seem a bit overwhelming. The ironic thing is that many of these questions will be answered in the chapter on namespaces, but many of the questions will answer themselves as you become more intimate with the language. They'll just start to "make sense," as we explore classes, scope, and other items which might warrant a namespace. But as many of you have asked the relevance of namespaces early, I'll post a brief example and explanation.

Purpose of Namespaces:

The single purpose of namespaces is to organize collections of classes, functions, and data types into a cohesive "block" which helps uniquely identify those elements as being different from other elements with the same name.

A Look at The Problem:

Lets say that you (John) and I (Jeromy) are creating games. We both need a 3D math library in order to perform all the cool mathy stuff we get to do when working in 3D. You (John) and I (Jeromy) both need classes that help us perform that math so we merrily create classes (We'll learn more about classes later) called Vector, Matrix, BoundingBox, etc....

Now, at some point You and I will go different directions with our libraries. For the sake of the discussion we'll say that You decide you want to include a physics component to your library. So you continue the development of your library with intricate classes on collision detection and response, intersection tests, etc....Meanwhile, I've decided to go a different route. I decided to immediately move into skeletal animation, so I've got cool classes including nodes for transform hierarchies, key frames, etc...both of our cool libraries are built upon the math part of our own libraries.

In all of our excitement we both publish our libraries to the Internet. As we're browsing "Mike's Emporium of Cool Game Libraries" we encounter each other’s libraries. I say "Wow, how cool would it be if my skeletons could collide with each other...," and you of course say "Wow, how cool would it be if I could make animated models collide rather than static meshes." So in all of our excitement we both download each other's libraries and attempt to build them...what happens?!?!

Well, we're likely to get a HUGE number of compiler errors indicating either "parameter list doesn’t match function declaration," "Multiply defined types" or just the plain old "function is too vague, could be either A, or B." So what's the problem? The problem is that You and I did not uniquely identify our classes so now that we've combined our libraries, the compiler doesn’t know whether I mean John's Matrix, or Jeromy's Matrix when it sees the word "Matrix" in the code.

There is a default solution, and before the advent of namespaces it was the only solution....Jeromy has to go through his local copy of John's library and change all the conflicting classes to a different name, or remove either his own or John's math library and convert it so that the rest of the library uses the other person's math classes. Sound like a lot of work?? IT IS!!...it's also time consuming and error prone as well.

Enter the Namespace:

Fortunately, there is a better solution. And that's to avoid the possibility of conflict from the beginning. As I'm developing my library I put it into a namespace which uniquely identifies me or the project. for example:

namespace Jeromy
{

}

Meanwhile, John puts his library into a namespace that uniquely identifies him or his project. For example:

namespace John
{

}

At this point both of our libraries are contained within a namespace. Now, as we work with our libraries we continue to address our classes, etc...by its local namespace. ie:

John::Matrix matrix1;

vs.

Jeromy::Matrix matrix1;

So how does this help us? Well, now when You and I get super excited and publish our content to the Internet for download we'll be pleasantly surprised to find that upon downloading each other’s code and building it, the compiler makes no noise at all? But how could this be? Well, because now we've told it which version of Matrix to use in the physics component, and which version to use in the animation component. So it needs no more clarification. As we continue on with our development, now involving BOTH libraries working together, we merely specify which classes to use at which time by either putting a using statement at the top, or by adding the namespace prefix to the beginning of all of the classes, etc...

The Standard Namespace:

The C++ ISO Board was kind enough to define for us a library of classes, templates, utility functions, etc...And to make sure that our content never collided with their content, they stuck all of their library into a namespace...The standard namespace. And for ease of use (Standard::cin would suck) they decided that the in-code name for the standard namespace was just:

namespace std
{

}

So whenever you prefix a class with "std" or whenever you use a "using" statement involving "std," you're accessing components of the Standard Library.

The Global Namespace:

Since we've now introduced the concept of a namespace, what do we do with all the stuff NOT in a namespace? Well, the answer is "There is no such thing." Your compiler treats all classes, function, data types, etc...not defined within a namespace as being in the same namespace - The Global Namespace. So anything not within in a namespace is prone to collide with anything else also "not within a namespace"...or put more correctly "anything left to be defined within the global namespace is prone to collide with anything else left to be defined within the global namespace."

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by Fruny
all the components of the standard C++ library have been placed in the std namespace. It is the way the library is organized.


So the standard C++ library is a collection of files (iostream, vector,...) which all are coded in this way:

namespace std {

/*all classes, varables and functions of this part of the standard C++ library, for example iostream*/

}

Is this right?

Essentially, yes. If you're curious, you can open the header files (e.g. iostream) and see for yourself. On my computer, they're in C:\Program Files\Microsoft Visual Studio 8\VC\include and C:\msys\mingw\include\c++\3.4.5
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Just wanna give you all who is working with this C++ workshop some credit! I think it's a great thing you're all working on here.

I'm sure I will pick up some nice advice myself along the way, though i'm quite proficient in C++, I find the discussion on the C++ standard interesting as i'm still a bit stuck on the old way. ;)

I hope to help and give some advice from time to time as this Workshop moves along.

Good Luck all, and keep coding! =)
Free Classic Arcade Games by Blitzway Games
I found the "Teach Yourself C++ in 21 Days, 2nd Edition" available on the internet and I guess it might help some people who don't have this book. I still curious if "Teach Yourself C++ in 21 Days, 2nd Edition" is much different than "Teach Yourself C++ in 21 Days, 5th Edition" or not.

Quote:
For the tutors: maybe you might wanna make a Tutors PM box and have the "students" post their assignments or extra credit there. i say this only because one posts and everyone copies (well not everyone but enough).

anyway, just a suggestion.


I afriad that I ain't a tutor in this C++ Workshop(I wish I could be but I am not good enought to be one). I gave those "Extra Exercises" only to help them test their understanding and have some fun while they are learning.

Quote: Original post by CryptoGeek
Great workshop folks! those extra execises were pretty cool! Is the next chapter Doom? ;)


Quote: Original post by warsen
thanks for expanding on the definitions from the book. invisal, thanks for the summary and the extra exercises.


I am really gald that you like my exercises. I will post more on next chapter. if the tutor allow me to.

Cheer!
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
Invisal,

Thanks for the link, but it has been posted many times...Also, you can check in my introductory post in the Q&A section about what to do regarding purchasing the book vs. using the online 2nd edition.

You can find that Q&A towards the bottom of my post Here.

As well, this is not the place to express your interest in being a tutor. There is This thread for that. Just follow the directions in the primary post.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Greetings All!

As there are roughly 5 days left before the end of Chapter 1 and Chapter 2, I thought it was just about QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 1 and 2. Each chapter builds upon the knowledge obtained in previous chapters, so it can be dangerous and confusing to advance to later chapters without a complete understanding of the material already covered.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any questions which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

Chapter 1
  • What is an interpreter?
  • What is a compiler?
  • Why is compiled code faster than translated code?
  • What does structured programming (procedural programming) consist of?
  • What were the primary problems object oriented programming was designed to solve?
  • What are the three pillars of object oriented programming? Does C++ support them all?
  • What is encapsulation?
  • What is inheritance?
  • What is polymorphism?
  • Should you learn C before C++, why or why not?
  • What is the FIRST question you ask when beginning your designs?
  • What is the SECOND question you ask when beginning your designs?
  • What are the two primary steps in creating an executable? What is the output of each step?
  • If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
Chapter 2
  • When you issue a command to compile your code, what is run first? What does it do?
  • What symbol indicates a preprocessor directive?
  • What does an "include" directive instruct the preprocessor to do?
  • What is the necessary function which all console C++ programs have?
  • Who calls that function?
  • Can you declare main as void? Why use int instead?
  • Are "curly braces" optional when enclosing a function?
  • What is the Standard Library?
  • How do you instruct the compiler that you want to use part of the Standard Library?
  • [Extra Credit] What's the difference between "\n" and endl?
  • What are the 3 ways to use a class in a namespace? Use "cout" as an example.
  • What are the two types of comments?
  • [Extra Credit] What is self-documenting code? Does that remove the need for comments?
  • Are the 3 components of a function declaration (function header)?
  • What do you call a function that is part of a class?
  • [Extra Credit]What does a compiler do with "white space?"

Good luck!

Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement