Welcome: Guest
The Home Of Programming Languages
Welcome to 876 Softwares, the home of programming languages.
Q&A Part III
May 07 2008 at 9:18 am 127
Q. How do you choose between recursion and iteration?
A. Some problems cry out for recursion, but most problems will yield to iteration as well. Put recursion in your back pocket; it may come in handy someday.
Q. Is it better to use while (1) or for (;;)?
A. There is no significant difference.
Q. Why are pointers so important?
A. Today you saw how pointers are used to hold the address of objects on the free store and how they are used to pass arguments by reference. In addition, on Day 13, "Polymorphism," you′ll see how pointers are used in class polymorphism.
Q. Why should I bother to declare anything on the free store?
A. Objects on the free store persist after the return of a function. Additionally, the ability to store objects on the free store enables you to decide at runtime how many objects you need, instead of having to declare this in advance. This is explored in greater depth tomorrow.
Q. Why should I declare an object const if it limits what I can do with it?
A. As a programmer, you want to enlist the compiler in helping you find bugs. One serious bug that is difficult to find is a function that changes an object in ways that aren′t obvious to the calling function. Declaring an object const prevents such changes.
Q. Why have references if pointers can do everything references can? A. References are easier to use and understand. The indirection is hidden, and there is no need to repeatedly dereference the variable. Q. Why have pointers if references are easier?
A. References cannot be null, and they cannot be reassigned. Pointers offer greater flexibility, but are slightly more difficult to use.
Q. Why would you ever return by value from a function?
A. If the object being returned is local, you must return by value or you will be returning a reference to a non-existent object.
Q. Given the danger in returning by reference, why not always return by value?
A. There is far greater efficiency in returning by reference. Memory is saved and the program runs faster.
Q. Why would you ever use default values when you can overload a function?
A. It is easier to maintain one function than two, and often easier to understand a function with default parameters than to study the bodies of two functions. Furthermore, updating one of the functions and neglecting to update the second is a common source of bugs.
Q. Given the problems with overloaded functions, why not always use default values instead?
A. Overloaded functions supply capabilities not available with default variables, such as varying the list of parameters by type rather than just by number.Q. When writing a class constructor, how do you decide what to put in the initialization and what to put in the body of the constructor?
A. A simple rule of thumb is to do as much as possible in the initialization phase--that is, initialize all member variables there. Some things, like computations and print statements, must be in the body of the constructor.Q. Can an overloaded function have a default parameter?
A. Yes. There is no reason not to combine these powerful features. One or more of the overloaded functions can have their own default values, following the normal rules for default variables in any function.
Q. Why are some member functions defined within the class declaration and others are not?
A. Defining the implementation of a member function within the declaration makes it inline. Generally, this is done only if the function is extremely simple. Note that you can also make a member function inline by using the keyword inline, even if the function is declared outside the class declaration.
Q. What happens if I write to element 25 in a 24-member array?
A. You will write to other memory, with potentially disastrous effects on your program.
Q. What is in an uninitialized array element?
A. Whatever happens to be in memory at a given time. The results of using this member without assigning a value are unpredictable.
Q. Can I combine arrays?
A. Yes. With simple arrays you can use pointers to combine them into a new, larger array. With strings you can use some of the built-in functions, such as strcat, to combine strings.
Q. Why should I create a linked list if an array will work?
A. An array must have a fixed size, whereas a linked list can be sized dynamically at runtime.
Q. Why would I ever use built-in arrays if I can make a better array class?
A. Built-in arrays are quick and easy to use.
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?