Welcome: Guest
The Home Of Programming Languages
Welcome to 876 Softwares, the home of programming languages.
Q&A Part II
May 07 2008 at 9:10 am 124
Q. Why not use literal constants; why go to the bother of using symbolic constants?
A. If you use the value in many places throughout your program, a symbolic constant allows all the values to change just by changing the one definition of the constant. Symbolic constants also speak for themselves. It might be hard to understand why a number is being multiplied by 360, but it′s much easier to understand what′s going on if the number is being multiplied by degreesInACircle.
Q. What happens if I assign a negative number to an unsigned variable? Consider the following line of code:
unsigned int aPositiveNumber = -1;
A. A good compiler will warn, but the assignment is legal. The negative number will be assessed as a bit pattern and assigned to the variable. The value of that variable will then be interpreted as an unsigned number. Thus, -1, whose bit pattern is 11111111 11111111 (0xFF in hex), will be assessed as the unsigned value 65,535. If this information confuses you, refer to Appendix C.
Q. Can I work with C++ without understanding bit patterns, binary arithmetic, and hexadecimal?
A. Yes, but not as effectively as if you do understand these topics. C++ does not do as good a job as some languages at "protecting" you from what the computer is really doing. This is actually a benefit, because it provides you with tremendous power that other languages don′t. As with any power tool, however, to get the most out of C++ you must understand how it works. Programmers who try to program in C++ without understanding the fundamentals of the binary system often are confused by their results.
Q. Why use unnecessary parentheses when precedence will determine which operators are acted on first?
A. Although it is true that the compiler will know the precedence and that a programmer can look up the precedence order, code that is easy to understand is easier to maintain.
Q. If the relational operators always return 1 or 0, why are other values considered true?
A. The relational operators return 1 or 0, but every expression returns a value, and those values can also be evaluated in an if statement. Here′s an example: if ( (x = a + b) == 35 ) This is a perfectly legal C++ statement. It evaluates to a value even if the sum of a and b is not equal to 35. Also note that x is assigned the value that is the sum of a and b in any case.
Q. What effect do tabs, spaces, and new lines have on the program?
A. Tabs, spaces, and new lines (known as whitespace) have no effect on the program, although judicious use of whitespace can make the program easier to read.
Q. Are negative numbers true or false?
A. All nonzero numbers, positive and negative, are true.
Q. Why not make all variables global?
A. There was a time when this was exactly how programming was done. As programs became more complex, however, it became very difficult to find bugs in programs because data could be corrupted by any of the functions--global data can be changed anywhere in the program. Years of experience have convinced programmers that data should be kept as local as possible, and access to changing that data should be narrowly defined.
Q. When should the keyword inline be used in a function prototype?
A. If the function is very small, no more than a line or two, and won′t be called from many places in your program, it is a candidate for inlining.
Q. Why aren′t changes to the value of function arguments reflected in the calling function?
A. Arguments passed to a function are passed by value. That means that the argument in the function is actually a copy of the original value. This concept is explained in depth in the "Extra Credit" section that follows the Workshop.
Q. If arguments are passed by value, what do I do if I need to reflect the changes back in the calling function?
A. On Day 8, pointers will be discussed. Use of pointers will solve this problem, as well as provide a way around the limitation of returning only a single value from a function.
Q. What happens if I have the following two functions?
int Area (int width, int length = 1); int Area (int size); Will these overload? There are a different number of parameters, but the first one has a default value.A. The declarations will compile, but if you invoke Area with one parameter you will receive a compile-time error: ambiguity between Area(int, int) and Area(int).Q. How do you choose between if/else and switch?
A. If there are more than just one or two else clauses, and all are testing the same value, consider using a switch statement.
Q. How do you choose between while and do...while?
A. If the body of the loop should always execute at least once, consider a do...while loop; otherwise, try to use the while loop.
Q. How do you choose between while and for?
A. If you are initializing a counting variable, testing that variable, and incrementing it each time through the loop, consider the for loop. If your variable is already initialized and is not incremented on each loop, a while loop may be the better choice.
Posted by administrator | Categories: C++ | Comments: 73
Advertisement 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Advertisement 2
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Advertisement 3
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam at libero. Lorem ipsum dolor sit amet, consectetuer adipiscing. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Offline
Did You Know?