MFC Programmer's SourceBook : Thinking in C++
Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

answers:

[[[ Readers: Questions have not been distributed through chapter yet. The questions will be repeated here, along with the answers . If you think of questions which might be useful to include, please note them.]]]

  1. 1. What is object oriented programming, in one sentence?
A: Sending messages to objects

  1. 2. In C++, how is the C language extended to embody this primary concept of object-oriented programming?
A: Functions are added to structs, so structures describe not only an agglomeration of data, but an agglomeration of data and functions. We often say this means that a structure has characteristics (described by the data) and behaviors (described by the functions).

  1. 3. What is the difference between a structure and an object?
A: A structure is a description of a type, and an object is a variable of that new type.

  1. 4. Can you compile C code with C++?
  2. 5. Does a structure occupy storage? Do the functions in a structure require storage for each function, in each object?
A: No. A member function is like an ordinary function except for its name, so there’s only one piece of storage allocated for that function regardless of how many variables you create. The only difference is that the name is scoped within the name of the structure.

  1. 6. What are the two basic differences between an ordinary C function and a member function (inside a structure) in C++?
A: The first is that name is scoped within the structure so it’s no longer a global name, and the second is that the address of the structure is secretly passed in as the first argument. Other than that, it’s just like an ordinary global function.

  1. 7. In a function definition, How do you tell the compiler “this is a member function of a particular class, and not an ordinary function definition.
A: With the scope resolution operator ::

  1. 8. What is a translation unit?
A: A single file which generates code. Generally, these have a file name extension of .cpp, to distinguish them from header files (with extensions of .h). The compiler is invoked once for each CPP file to create an object module (this name is an artifact, it is the objective of the compilation rather than anything object-oriented).

  1. 9. What does the linker do?
A: The primary job of the linker is to resolve references between function calls. These references often span a group of translation units and may also include various libraries (a file that often has the extension LIB), which are collections of OBJ files. The linker takes all these and binds them into an executable which contains the necessary code to resolve the function calls.

  1. 10. How does the linker tell the difference between an ordinary C function call, and a call to a member function which happens to have the same name?
A: When the compiler generates internal names, it does something which is often referred to as name mangling . When a function is a member of a structure, the name of the structure is mangled in with the name of the function, thereby creating an internal name which is completely distinct from a function of the same name that is global, or one that is a member of another structure. Therefore the linker cannot confuse the two, and neither can the compiler.

  1. 11. How does a member function know which object it is supposed to be working on? That is, what syntax do you use if you have an object and want to call a member function for that object, rather than for another object?
A: Just like in C, if you have a structure and you want to select a member of that structure, you give the name of the variable, followed by the ‘ .’ (dot) operator, followed by the name of the member function. Of course, since it’s a function, it is followed by the argument list.

  1. 12. What syntax do you use if you have a pointer to a structure, and you want to call a member function?
A: Just like in C, if you have a pointer to a structure and you want to select a member of that structure, you use the ‘ ->‘ (arrow) operator. Again, since it’s a member function you must use the argument list. Also, since it’s a function it can have a return value.

  1. 13. What kind of code does the compiler generate when you call a member function? That is, how is the information passed into the function about what object is called?
A: The information about what object is called is secretly passed in as the first argument of the function. That is, whenever you have a member function you do not explicitly put down the fact that the first argument of the member function is the address of the object, but the compiler generates that information because it knows it’s looking at a member function. Also, when you call the member function for a particular object, the compiler takes the address of that object and passes that address as the first argument to the member function. All this happens quietly. You can think of this whole process as the compiler generating some extra code for you so you can use a more convenient syntax.

  1. 14. What is the name of the address of the object, referred to from inside the member function?
A: The name the keyword this. Inside a member function (and only inside a member function) the word this means “the starting address of the current object.” The code that selects members using this is a much closer approximation to what the compiler generates when you make an ordinary member function call.

  1. 15. How do you call a member function from within another member function?
A: You simply call it. Since you’re inside a member function of that class, the compiler first looks for another member function of the same class before it goes looking for a global function of the same name.

  1. 16. Suppose you have a class, and you're inside a function f(), and there's a member function of the same class called g(), and there's also a global function g(). How do you call the global function g() so the member function isn't automatically called instead.
A: Use the global scope-resolution operator, that is the scope-resolution operator with nothing preceding the operator. This refers to the global version of a function.

  1. 17. How does the compiler insure the consistency of structures across translation units?
A: The compiler implicitly forces you to put structure declarations (that is, descriptions of the structures) inside header files. The compiler must see the description of the structure before you can use that class by either declaring a variable of that class (that is, an object), or call a member function for that variable. In addition, the compiler complains if it sees a multiple declaration. Therefore, the intelligent thing to do is put the declaration of a class into a single header file, and put the definitions into a single CPP file. Then everywhere you use that class you must include the header file.

  1. 18. In C, you must use typedef if you want to be able to treat a struct name as if it were a built-in type. This is a common practice in C programming, especially when creating libraries. Is this necessary in C++? How does C++ treat the same situation?
A: The C++ compiler automatically creates the equivalent of a typedef (although it generally treats it differently internally). You can then treat the structure name as if it were a true native type.

  1. 19. Why does the compiler only allow you to declare a structure once during the compilation of a particular translation unit?
A: If it saw the declaration more than once, it wouldn’t know which was the correct declaration. Thus, it prevents you from redeclaring a class.

  1. 20. It's possible with multiple levels of #includes that a class can be declared more than once. What is the common practice used to prevent the accidental multiple declaration of a class?
A: The preprocessor is ordinarily used by creating a “mangled name” of your own, generally by using the name of the file and the extension, and mixing in underscores. Leading underscores are not used because those are reserved in the Standard C library.

  1. 21. How does the combination of header files and the requirement that you must declare header files before they are used increase the safety of your programs?
A: C allows you to call functions without first declaring them. C++ effectively forces you to place the class declaration in a header file (although you could certainly not include header files and just declare the class by hand in each translation unit where it is used – but that would be ridiculous) and you have to declare functions with prototypes before calling them. This way, the compiler can insure the function is called properly including proper type checking on the arguments. Since the information wasn’t there in C, it was possible to make a mis-call which was a very subtle and difficult error to detect. In C++ you get an error message.

  1. 22. What is the difference between a declaration and a definition?

    A:

  2. 23. What should you never put into a header file?
A: Although there are a few semi-exceptions (which have their own sensibility, and which you’ll see throughout the book), you should never put anything into a header file which causes the compiler to allocate storage. This includes variable/objects and function bodies. Put another way, only declarations should go into header files, never definitions. The linker must never see multiple definitions for the same piece of storage. If you put a function definition, or an object definition into a header file, then that storage will be allocated anywhere the header file is included. Then when the linker comes along to create the final program by resolving all the references, it will find multiple definitions for the object/variable and cause a multiple definition error. There must only be a single definition for a particular function or variable.

  1. 24. When the C++ compiler sees a structure which was originally written for a C compiler, what does it do differently than what the C compiler does?
A: The only difference is the implied typedef performed for the structure’s tagname.

  1. 25. Why is the semicolon at the end of a structure declaration so important?

    A: The semicolon indicates the end of all object definitions for this particular structure. If the semicolon isn't there, the next name the compiler sees it will assume is the definition of an object of that structure type.

  2. 26. What's the difference between C and C++ prototypes?

Contents | Prev | Next


Go to CodeGuru.com
Contact: webmaster@codeguru.com
© Copyright 1997-1999 CodeGuru