🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - C++ Keywords, Variables, & Constants (Ch. 3)

Started by
65 comments, last by Dbproguy 16 years, 2 months ago
i have always just included the header file in the source and let the header file have the declarations that the source file needs. i guess jwalsh, you are saying that this is backwards? i guess i have always done it this way and never really thought about it much.

Since i also usually program using classes my header files usually have multiple files as i define a class in a header file but also use inheritance in this class so naturally my header file will have to include the header files associated with these classes that i am inheriting.

do i have the right idea here or what am i doing wrong?
heh
Advertisement
Quote: Original post by OpenGL_Guru
i have always just included the header file in the source and let the header file have the declarations that the source file needs. i guess jwalsh, you are saying that this is backwards? i guess i have always done it this way and never really thought about it much.

Since i also usually program using classes my header files usually have multiple files as i define a class in a header file but also use inheritance in this class so naturally my header file will have to include the header files associated with these classes that i am inheriting.

do i have the right idea here or what am i doing wrong?


It sounds like you have this alright. When working with classes its common to "Declare" the class in the header file (1 class per header file) and then Define the methods of the class within the matching .ccp file.

If you're using inheritance than the class declaration REQUIRES an #include of the base class, because the compiler needs to know more than just the declaration of the base class when creating the derived class.

At this point, however, we're not yet covering classes, so this question is a bit advanced for the audience. Please direct further questions of this nature to the "For Beginners" or "General Programming" forums, until we've reached such a point that the readers will understand the answers.

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
All,

I'd like to take a moment and apologize to the viewers. As you have probably noticed there are readers within this forum of all skill levels and unfortunately I've done a poor job re-directing the more advanced people to other resources for the time being. I will try and do better.

It occurs to me there's been a cry by the readers to slow down with respect to our responses. This IS a beginning C++ forum, and we've yet to go beyond the declaration of variables. Really, even the discussion of headers is a bit premature at this point. So I'd like to ask the tutors and moderators reading the forum to kindly ask people to take the more advanced questions to another forum if they're not relevant to the weekly thread which the questions were posted in. We need to remember that there are people following this workshop who've never programmed before, so we should keep our answers concise and precise.

As well, I want to remind the tutors and moderators to be liberal with their use of [opinion/] tags. This is an educational forum, not philosophical. For example...

[opinion]
I don’t believe that C++ is necessarily circular in learning, nor is this apparent learning curve a problem with C++. It's a common misconception that C++ is a more difficult language to learn than perhaps C# or Java. When you ask the 'experts' who claim C++ is more difficult they're rarely able to give you a solid reason for why it's more difficult. In truth it’s a problem with all object oriented languages which derive from C. The principles, semantics, and syntax which you learn at early stages apply equally well to classes, and the more advanced topics. As a result, it’s common for people to feel they're re-learning the information, or they feel the need to move faster than is strictly good for their understanding.

As well, most 'experts' who've been programming for any length of time learned C++ before Java or C#. As a result C++ WAS more difficult, because they didn’t have the experience of already having learned a nearly identical language. The few reasons people site for C++ being "So difficult" are often their own recollection of the problems they suffered while learning the language. No doubt, they had finally grasped the concepts before moving on to C# or Java, and thus they were "easier" languages to learn. In reality, all OO languages are equally difficult.
[/opinion]

Note: The above "opinion" statement was simply made to counteract the negative philosophical discussion began about the difficulties of learning C++. Please do not discourage beginning programmers by indicating either the language may be too difficult for them, or that there's an easier alternative. Please be encouraging.

As well, please discontinue discussion about the "quality" of the language at this time. It's not that I don’t think discussions are important, it's just that such discussions make it impossible to be objective, and I don’t want to persuade the readers one way or the other. I'd like them to determine for themselves the quality and ease of learning the C++ language.

Thanks Everyone!
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 jwalsh
Quote: Original post by OpenGL_Guru
i have always just included the header file in the source and let the header file have the declarations that the source file needs. i guess jwalsh, you are saying that this is backwards? i guess i have always done it this way and never really thought about it much.

Since i also usually program using classes my header files usually have multiple files as i define a class in a header file but also use inheritance in this class so naturally my header file will have to include the header files associated with these classes that i am inheriting.

do i have the right idea here or what am i doing wrong?


It sounds like you have this alright. When working with classes its common to "Declare" the class in the header file (1 class per header file) and then Define the methods of the class within the matching .ccp file.

If you're using inheritance than the class declaration REQUIRES an #include of the base class, because the compiler needs to know more than just the declaration of the base class when creating the derived class.

At this point, however, we're not yet covering classes, so this question is a bit advanced for the audience. Please direct further questions of this nature to the "For Beginners" or "General Programming" forums, until we've reached such a point that the readers will understand the answers.

Cheers!


sorry about that jwalsh -- i guess i shouldnt have gone that far but from what i read, maybe i assumed you meant that all the source files should have the includes in it and not the accompanying header file, when in fact i usually just define various other header files the source will use IN the main header file itself and then in the source file include the header file. so for example:

src.cpp

#include src.h

*in src.h*
include 3 or 4 header files needed in the src file.

In my first post above thats basically what i was telling you that i do and you said that i basically had the right idea BUT also it seemed that you were trying to say that the src.cpp should have all the include files. I guess i am just wanted to see if you verify what you meant.. thanks!

heh
OpenGL_Guru,

Looks like I was misunderstanding you...What you're doing IS "incorrect."

Refer to my post to adam23 with "This is bad m'kay" at the top in order to figure out why. If you've got further questions about header files, please post them in another forum or send me a Private Message. This topic is currently beyond the scope of this thread.

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 it is Thursday, it's once again 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 chapter 3. 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.

In addition to the quiz questions below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

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

A new thread will be created in a day or two for you to post your answers to these question.

Chapter 3 Quiz

1. What is a variable?
2. What happens to a variable when the program terminates?
3. What purpose does the "label" or identifier serve with respect to variables?
4. What purpose does the type serve with respect to variables?
5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean?
6. Does an integer require the same amount of memory each time a program is run on the SAME computer?
7. Does an integer require the same amount of memory on EVERY computer?
8. What can be stored in a 'char' variable type? Integers, characters, both?
9. How large is a 'char' variable usually
9. What are the usual sizes for 'short int', 'int', and 'long int' data types?
10. What does the C++ Standard say about the size of integers?
11. How big are integers on a 32bit x86 processor using a modern compiler?
12. What operator can you use to determine the size of a data type?
13. Are integers signed or unsigned by default?
14. What are the minimum and maximum values for signed and unsigned long integers (4 byte integer)?
15. [Extra Credit] Why are those the minimum/maximum values? That is, how does the C++ language treat signed vs. unsigned integers differently on a "binary" level?
16. Is C++ a case-sensitive language? What does this mean? Show some examples.
17. What are the Do's and Dont's of naming variables according to your textbook?
18. What operator do you use to assign a value to a variable?
19. Can you initialize a variable at the same time it is declared? What does this look like?
20. What does the "typedef" keyword do? Why is it useful?
21. When do you use short, int, or long?
22. What happens when you attempt to store a value bigger than a variable can hold?
23. What happens when you attempt to store a value smaller than a variable can hold?
24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things?
25. When must you initialize constants?
26. What is the difference between a literal constant, and a symbolic constant? Show some examples.
27. What are the two ways covered in this chapter for declaring a constant? Which is better?
28. How do you declare an enumeration?
29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type.

Cheers ang 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
Heya all!

If anyone has questions regarding the above quiz, please dont hesitate to ask. We want to make sure everyone was able to answer them sucessfully. If you WERE able to answer them, feel free to post or check your answers in the answer thread...There.

We will move on to chapter 4 on Monday morning.

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
I am little late and still going through the chapter but I would like to know little more in detail about 7 question.

I read in the book that it depends on compiler being used and also on CPU. 32bit, 64bit?

Advanced compiler can make difference but how come cpu/hardware. I thought only in games which are graphically intense require good hardware. But integer memory are also dependent(are these used in making games)[totally].

One more question, how can I know what operators and function Dev-C++ has. Like in one eg: it uses sizeof(int). To find length of a string in one book I saw. It was asked to do using loop why[dead].
Quote: Original post by kimi
I read in the book that it depends on compiler being used and also on CPU. 32bit, 64bit?


Correct. The sizes of the integral types are platform-dependent, with a few guarantees, such as sizeof(char) == 1.

Quote: Advanced compiler can make difference but how come cpu/hardware. I thought only in games which are graphically intense require good hardware. But integer memory are also dependent(are these used in making games)[totally].


Remember that games are not the main purpose for computers. There is a lot of specialized hardware used for numerical computation (i.e. supercomputers). The sizes of the data types will obviously have to be made to match what the computer can handle easily, for efficiency reasons, if nothing else.

Quote: One more question, how can I know what operators and function Dev-C++ has.


Dev-C++ is an IDE that uses a Windows port (MinGW32) of GCC (version 3.4.2). You can check the GCC manual if you have questions about GCC-specific details.

Quote: Like in one eg: it uses sizeof(int). To find length of a string in one book I saw. It was asked to do using loop why[dead].


As for "what operators and functions", well, GCC aims to implement ISO C++, so any halfway decent C++ reference manual would be fine. Anyway, learning all those things is what we are trying to do here. [grin]

You can check MSDN. Or the Dinkumware website (note that it documents functions which aren't necessarily supported by mainstream compilers yet - namely the ISO Technical Reports).

"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
Quote: Original post by kimi
I am little late and still going through the chapter but I would like to know little more in detail about 7 question.

I read in the book that it depends on compiler being used and also on CPU. 32bit, 64bit?

Advanced compiler can make difference but how come cpu/hardware. I thought only in games which are graphically intense require good hardware. But integer memory are also dependent(are these used in making games)[totally].

One more question, how can I know what operators and function Dev-C++ has. Like in one eg: it uses sizeof(int). To find length of a string in one book I saw. It was asked to do using loop why[dead].


Basically, each processor is designed to work with so many bits in parallel. Most current processors will use 32 bits (4 bytes), so the normal size of an integer is also 4 bytes. However, in the future, when 64 bit computers are mainstream, 64 bit integers may become the norm. It is important to remember this when distributing code that compile on any platform. If I had old code that relied on an integer being 2 bytes, It may not function when I compile the same code on a current compiler.

Not sure why you need to use sizeof to find the size of a string (though I bet it was a C-style char* string). Again, strings are a bit more advanced than where we are here. Think of strings as a sequence of characters (which is what they are). Later, we will be able to make them a little more clear. To find the length, they used a loop to go through each character and find the last one, then figure out how long it is. Again, strings, as well as loops, are a few chapters away, so be patient.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute

This topic is closed to new replies.

Advertisement