1、 Viewing Hints Book Home Page Free Newsletter Seminars Seminars on CD ROM Consulting Annotated Solution GuideRevision 1.0for Thinking in C+, 2nd edition, Volume 1by Chuck Allison2001 MindView, Inc. All Rights Reserved. Previous Chapter Table of Contents Next Chapter Chapter 44-1In the Standard C lib
2、rary, the function puts( ) prints a char array to the console (so you can say puts(“hello“). Write a C program that uses puts( )but does not include or otherwise declare the function. Compile this program with your C compiler. (Some C+ compilers are not distinct from their C compilers; in this case
3、you may need to discover a command-line flag that forces a C compilation.) Now compile it with the C+ compiler and note the difference.(Left to the reader)4-2Create a struct declaration with a single member function, then create a definition for that member function. Create an object of your new dat
4、a type, and call the member function.(see the next exercise)4-3Change your solution to Exercise 2 so the struct is declared in a properly “guarded” header file, with the definition in one cpp file and your main( ) in another.Solution:/: S04:MyStruct.h#ifndef MYSTRUCT_H#define MYSTRUCT_Hstruct MyStru
5、ct void f();#endif /:/: S04:MyStruct.cpp O#include “MyStruct.h“#include using namespace std;void MyStruct:f() cout using namespace std;struct HasInt int x;void setInt(int newX) x = newX;int getInt() return x;int main() HasInt h;h.setInt(5);cout using namespace std;struct HasInt int x;void setInt(int
6、 x) this-x = x;int getInt() return this-x;void display() cout getInt() by saying: printf(“%dn”, i) in which i is the int you wish to print. Create a separate file containing main( ), and in this file declare f( ) to take a float argument. Call f( ) from inside main( ). Try to compile and link your p
7、rogram with the C+ compiler and see what happens. Now compile and link the program using the C compiler, and see what happens when it runs. Explain the behavior.Solution:This exercise illustrates type-safe linkage. In C+ function name are decorated (aka mangled) to include information about their ar
8、guments. The consequence is that the name the linker sees is actually quite different than what you see. For example, f(int) might appear to the linker as f_F1i (a function taking one int), say, and f(double) as f_F1d. Since the names are distinct, you get a linker error. In C the names are the same
9、, allowing you to call f(double) with an int, which is an error.4-10Find out how to produce assembly language from your C and C+ compilers. Write a function in C and a struct with a single member function in C+. Produce assembly language from each and find the function names that are produced by you
10、r C function and your C+ member function, so you can see what sort of name decoration occurs inside the compiler.Solution:For f(int) one compiler generated the name ?fYAXHZ, and ?fYAXNZ for f(double). Go figure.4-11Write a program with conditionally-compiled code in main( ), so that when a preproces
11、sor value is defined one message is printed, but when it is not defined another message is printed. Compile this code experimenting with a #define within the program, then discover the way your compiler takes preprocessor definitions on the command line and experiment with that.(Left to the reader)4
12、-12Write a program that uses assert( ) with an argument that is always false (zero) to see what happens when you run it. Now compile it with #define NDEBUG and run it again to see the difference.(Left to the reader)4-13Create an abstract data type that represents a videotape in a video rental store.
13、 Try to consider all the data and operations that may be necessary for the Videotype to work well within the video rental management system. Include a print( ) member function that displays information about the Video.(Left to the reader)4-14Create a Stack object to hold the Video objects from Exerc
14、ise 13. Create several Video objects, store them in the Stack, then display them using Video:print( ).(Left to the reader)4-15Write a program that prints out all the sizes for the fundamental data types on your computer using sizeof( ).Solution:/: S04:Sizeof.cpp#include int main() using namespace st
15、d;cout as its underlying data structure.(Left to the reader)4-17Dynamically create pieces of storage of the following types, using new: int, long, an array of 100 chars, an array of 100 floats. Print the addresses of these and then free the storage using delete.Solution:/: S04:Storage.cpp#include int main() using namespace std;int* p_int = new int;long* p_long = new long;char* p_chars = new char100;float* p_floats = new float100;cout (p_chars) endl;cout “p_floats = “ p_floats endl;delete p_int;delete p_long;delete p_chars;delete p_floats;/* Output:/ Compiler A: