123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|541|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> C++ class, Week 2

Sat, 03 Oct 2009, 05:31
Afr0
We're still reading 'C++ Programming for the absolute beginner'.
This week, we're been covering Chapter 2, and now's the time for people to write their answers to the tasks at the end of the chapter!
Sorry for the delayed topic, I'm still reading Chapter 2 myself, as this has been a busy week for me (I've also been sick).

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Sat, 03 Oct 2009, 14:10
Afr0
Another point not mentioned in the book;

You cannot directly initialize variables 'on the fly' inside of class declarations - unless the variable is constant or static.



Another exception is for static classes:



This will most likely be taught by the book later on when you start learning about classes.

Edit: I'll be posting my solutions to the tasks tommorrow when I've had a chance to read through Chapter 2.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Sun, 04 Oct 2009, 07:00
Afr0


1.
Number of books in a bookshelf: unsigned short
The cost of this book: float
The number of people in the world: float (long long wouldn't actually be large enough!)
The word Hello: string

2.
NumBooks
CostOfBook
NumPeopleInWorld
Hello

3.
Constants provide for easy variable replacement in case of a value change
Code is more understandable (in some (most?) cases) than when using literals

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Thu, 08 Oct 2009, 12:24
Phoenix
Afr0 You cannot directly initialize variables 'on the fly' inside of class declarations - unless the variable is constant or static.

Nope. It has to be static and constant and integral (char, int, short, etc.).

Afr0 Another exception is for static classes

Static classes are only present in C#.
Sat, 10 Oct 2009, 05:48
Spare
1.
bookshelf: unsigned short
costs: float
number of people: float
the word hello: String

2.
bookCount
bookPrice
peopleCount
HELLO

3.
One advantage is that if you use a value in your program that will be the same all the time, you only have to change it once, in case you have to change it!
Another reason... I cannot think of.

4.


5. *cut* I was wrong!
Sat, 10 Oct 2009, 07:35
Phoenix
Here are some extra questions for you all, which hopefully are a bit more difficult.

1. What is the value of 'a'?

2. Why is it most often wrong to use #define macros?
3. What is the difference between std::string and and a char pointer? Why should one almost exclusively use the former?
4. Write a function which converts a decimal number (base 10) to a base n number, such as a hexadecimal or octal one.
Sat, 10 Oct 2009, 09:07
Afr0
1. 5
2. Not sure
3. std::string is a template class with lots of inbuilt functions for string manipulation. char pointers have to be deleted after use.


-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Sat, 10 Oct 2009, 10:27
Spare
1.
I'd say 6, unless it's a trap that would make it 5.

2.
I don't see why you would use a macro. I mean, I guess it's better to get used to the real name of the function

3.
std::string = C++
char* = C

Of what I understand from the chats about this subject is that it's the best to only use char pointers when C functions need it, as they dont eat std::strings.


4. To be continued...
Sat, 10 Oct 2009, 15:19
Phoenix
I've realized that these problems might have been a bit too much, especially when writing the answers. I apologize.

Answers, for the curious, come here either way.
  1. This is a bit in-depth. Put shortly: the behaviour is undefined, because there are no rules to determine if the variable is to be incremented before or after the assignment.

    It seems there's been some discussion in the chat regarding the postfix/prefix operators. In theory, it is intended that the prefix increment is called before the variable is used, whereas the postfix increment is called at the next sequence point. A sequence point occurs at the end of a statement (semicolon), at the symbols of the ternary operator (short-hand if, a?b:c), at the && and || operators, amongst some other places. In the code posted above, there is no sequence point to distinguish which operation occurs first: the assignment or the increment operation. Therefore, it is not really defined, which makes this question a trick question.

    To achieve the difference between the postfix and prefix operators when the compiler generates assembly instructions from the code, of course different instructions have to be generated. In general, the postincrement is slower because the it has to create a temporary copy of the variable, increment, and then return the copy. The preincrement operator can simply increment and then return the variable.

    Any intelligent compiler will however in practice eliminate any difference when dealing with built-in types. The difference, however, remains when dealing with other types, such as the iterators found in the standard library. This is why the the general advice is to always use the prefix operator. Although optimizations occur sometimes they don't always, and when they don't it doesn't hurt to have picked the better alternative, however trivial it may be.

  2. Historically, preprocessor macros (#define <something>) have been used extensively, both in place of constants and inline functions. As constants, macros are inferior to regular constants because they offer no type safety. Constant variables, as opposed to macros, also have a scope. Since all preprocessor macros are global, they can sometimes incur some annoying name conflicts.

    As functions, macros are bad because they aren't really functions, but just a copy-n-paste on behalf of the compiler. Therefore they introduce a whole batch of problems which I will save myself from stating here, by pointing to the Parashift C++ FAQ: 1, 2, 3, 4.

  3. Spare and Afr0 partly got the answer here. Char arrays are artifacts of the C days. Dealing with them is unpleasant, because of many reasons. In terms of advantages, the std::string is superior because it offers more functionality, in a more elegant, flexible, and safer way.

    For instance, when dealing with raw character sequences, one is (often, but not always) dealing with dynamic memory and pointers; two concepts very prone to bugs. std::string automatically grows when needed. Standard char pointers need to be reallocated. Standard library iterators will scream and shout when you reach beyond the end of the string, whereas a pointer beyond the end of a character sequence somewhere in memory will not.

  4. This is more of a thinking exercise than a C++ annoyance. Here's my solution, working from base 2 to 16.


I think that you should move on with a faster pace. Spare is already on chapter 8, I've heard. And you're not really beginning programmers.