You are here:

C++/Data Types


Hey, Joseph, thanks for your detailed answer last time, they were really helpful. I got some more questions this time, do you think you can help me with some, thanks.

1)   In the following piece of code, does the default argument has to be initialized in the prototype? Can the function itself contain the argument? Also, it is necessary to state the actions performed when default argument is used?
Void f(char *s1, char *s2, int len = 0) {
 While (*s1) s1++; // what does this do? Is every (sth) a condition that stops when it reaches 0?
 If (len == 0) len = strlen (s2);
 While (*s2 && len) { // what does this condition do?
    *s1 = *s2;
    len--; }
 *s1 = ‘\0’; // why is this needed?

2)   What is a named constant? How is it different from string constants and other constants?
3)   What are the available types of objects and what are the relationships between data types, modifier, identifiers, qualifiers, variables, objects…?
4)   One of the projects on the book is created by separating them into 2 files, which are connected in some way (because it is demonstrating the use of extern), how can I create 2 separate files that is connected using Microsoft Visual Basic C++?
5)   An object is said to have optimized speed when it is stored using register keyword, where exactly is it stored? (I have little knowledge about the hardware of the computer)
6)   This question is about enumeration, are the following equal? If not, please tell me how the variable list pair up with the value-list.
Enum things {apple, bee, company} community, nation, world;

Enum things {apple, bee, company};
Community = apple;
Nation = bee;
World = company;

Thank you so much,

1. Default arguments can be supplied in the prototype or function definition, but they must be visible to the caller, which is generally only the prototype, so it's really best to define default parameters in the prototype.  You cannot define them in both places, because that doesn't work.  I'm not quite sure what you mean about stating the actions performed when default argument is used.

   while (*s1) s1++;

This line takes the pointer to the first string, s1, and moves the pointer to the end of the string.  A string in C++ is terminated with a NULL terminator, '\0', which equates to 0.  So, you can tell you're at the end of a string when the value of the character you are examining is 0.  

   while (*s2 && len)

This conditional keeps looping until one of two things happens: either you hit the end of the string s2, or the len value is 0.  The way the function is set up, the user can specify an amount to copy.  If they do specify, it's possible that they specify an amount smaller than the size of s2.  In this case, len == 0 will be the terminating condition of the loop.  If, however, they specify an amount larger than the length of s2, *s2 == 0 will be the terminating condition of the loop.  If the user specifies no length, a length of 0, or a length equal to the length of s2, then both conditions will fail simultaneously and end the loop.

   *s1 = ‘\0’;

As I stated previously, strings must terminate with a NULL terminator, '\0'.  When performing string manipulation such as copies, concatenations (what this function is doing), etc., it is good practice to self-terminate the string being manipulated.  Often times it will terminate itself appropriately, but just in case it doesn't, it's a good idea to go ahead and put one at the end yourself.

2. A named constant is simply a constant variable (oxymoron, I know... I'll explain).  It's a way of defining a "variable" with a constant value.  You literally cannot change the value contained within the variable.  It is a good idea, any time you have a constant value, to either declare a named constant or a define.  This way, you can change the value of the constant in one place and recompile the program.  If you scatter magic numbers about in your code, it's quite easy to forget to change one when you change the others.  Let's look at an example of a named constant:

   const float Pi = 3.141592f;

You can see the const keyword there.  That is what declares the "variable" Pi to be a constant.  This way, you can simply use Pi in your code without having to re-type 3.141592f.  Plus, if you decide to change the value of Pi (ok, bad example), you only have to change it in one place.

3. I'm not sure I understand the question, I'm sorry.  I'll try to answer, but if this isn't applicable, please try to rephrase the question for me.

An object is more of a concept.  Any time you write a class, you are defining an object.  A struct, too, can be considered to be an object (especially in C++, where the behavior of structs closely resembles the behavior of classes).

The rest, data types, modifiers, identifiers, qualifiers, variables, objects... this all pretty much boils down to jargon.  I suppose it's useful to know a qualifier versus a modifier, that sort of thing, but the most important thing to know is what each keyword does.  Knowing that the name of a class is its identifier is rather irrelevant.  Knowing that the virtual keyword is a modifier is irrelevant.

Sorry if this doesn't fully answer your question.  If you want me to try to explain further or if I completely missed the mark on this one, just let me know.

4. When you first start working in multiple files, it can be a bit scary.  You will quickly find it is second nature, however, and quite preferable to working in a single file.

In Visual Studio, the easiest way to add a file to the project is through the Solution Explorer.  In the Solution Explorer, you'll see each project that is a part of the solution.  In each project, you will see filters (seen as folders) and files.  Often times, there are no files directly beneath the project, and instead, they all exist within filters.  This is a nice way to organize things, but it's really up to you how you organize them.

By default in Visual Studio, projects have three filters: Header Files, Resource Files, Source Files.  With this organization structure, the idea is to put all .h (header) files into the Header Files filter.  All .cpp (source) files exist in the Source Files filter.

Sticking to this structure, if you want to add a new source file, right click on the Source Files filter and select Add, New Item.  From within this dialog, you will select Code, C++ File (.cpp).  If you instead wish to add a header file, you'll follow mostly the same instructions, but you'll right click on the Header Files filter and instead of choosing C++ File (.cpp), you'll choose Header File (.h).

5. This really isn't something you'll want to mess with right now.  This is really advanced.  That said, the register keyword attempts to (notice, attempts, not guaranteed) place the object in question into a CPU register.  When data is manipulated by the CPU, it is first loaded into a register on the CPU.  This means that something in main memory is loaded to a register, manipulated, then the results are stored back into main memory.  By keeping data in a register at all times, it significantly decreases access time and speeds up operations.  Again, though, generally, you don't want to mess with this.  It's really advanced stuff and you'll want to just sort of leave that to be worked out naturally.

6. Wow, that first line is strange.  I've never seen anyone do that.  Basically, you're creating an enumeration, then declaring three variables of that type of enumeration.  They are uninitialized, so contain invalid data.

The second set of lines declare the same enum.  However, this code would not compile as given.  You'd have to preface each of the last three lines with things:

   enum things {apple, bee, company};
   things community = apple;
   things nation = bee;
   things world = company;

You could alter the code slightly to make it more like the first and still initialize everything:

   enum things {apple, bee, company}
   community = apple,
   nation = bee,
   world = company;

This code, of course, doesn't have to be split on different lines.  It can all be a single line, if you prefer.

As always, if you don't understand something I've written or you have further questions, just let me know!


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.