首页 ANSWERS

ANSWERS

举报
开通vip

ANSWERSC++ A Beginner’s Guide by Herbert Schildt Answers to Mastery Checks Module 1: C++ Fundamentals 1. C++ is at the center of modern programming because it was derived from C and is the parent of Java and C#. These are the four most important programming l...

ANSWERS
C++ A Beginner’s Guide by Herbert Schildt Answers to Mastery Checks Module 1: C++ Fundamentals 1. C++ is at the center of modern programming because it was derived from C and is the parent of Java and C#. These are the four most important programming languages. 2. True, a C++ compiler produces code that can be directly executed by the computer. 3. Encapsulation, polymorphism, and inheritance are the three guiding principles of OOP. 4. C++ programs begin execution at main( ). 5. A header contains information used by the program. 6. is the header the supports I/O. The statement includes the header in a program. 7. A namespace is a declarative region in which various program elements can be placed. Elements declared in one namespace are separate from elements declared in another. 8. A variable is a named memory location. The contents of a variable can be changed during the execution of a program. 9. The invalid variables are d and e. Variable names cannot begin with a digit or be the same as a C++ keyword. 10. A single-line comment begins with // and ends at the end of the line. A multiline comment begins with /* and ends with */. 11. The general form of the if: if(condition) statement; The general form of the for: for(initialization; condition; increment) statement; 12. A block of code is started with a { and ended with a }. 13. // Show a table of Earth to Moon weights. 14. // Convert Jovian years to Earth years. 15. When a function is called, program control transfers to that function. 16. // Average the absolute values of 5 numbers. Module 2: Introducing Data Types and Operators 1. The C++ integer types are The type char can also be used as an integer type. 2. 12.2 is type double. 3. A bool variable can be either true or false. 4. The long integer type is long int, or just long. 5. The \t sequence represents a tab. The \b rings the bell. 6. True, a string is surrounded by double quotes. 7. The hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. 8. To initialize a variable, use this general form: type var = value; 9. The % is the modulus operator. It returns the remainder of an integer division. It cannot be used on floating-point values. 10. When the increment operator precedes its operand, C++ will perform the corresponding operation prior to obtaining the operand's value for use by the rest of the expression. If the operator follows its operand, then C++ will obtain the operand's value before incrementing. 11. A, C, and E 12. x += 12; 13. A cast is an explicit type conversion. 14. Here is one way to find the primes between 1 and 100. There are, of course, other solutions. Module 3: Program Control Statements 1. // Count periods. 2. Yes. If there is no break statement concluding a case sequence, then execution will continue on into the next case. A break statement prevents this from happening. 3. 4. The last else associates with the outer if, which is the nearest if at the same level as the else. 5. for(int i = 1000; i >= 0; i -= 2) // ... 6. No. According to the ANSI/ISO C++ Standard, i is not known outside of the for loop in which it is declared. (Note that some compilers may handle this differently.) 7. A break causes termination of its immediately enclosing loop or switch statement. 8. After break executes, “after while” is displayed. 9. 10. 11. // Change case. 12. C++’s unconditional jump statement is the goto. Module 4: Arrays, Strings, and Pointers 1. short int hightemps[31]; 2. zero 3. // Find duplicates 4. A null-terminated string is an array of characters that ends with a null. 5. // Ignore case when comparing strings. 6. When using strcat( ), the recipient array must be large enough to hold the contents of both strings. 7. In a multidimensional array, each index is specified within its own set of brackets. 8. int nums[] = {5, 66, 88}; 9. An unsized array declaration ensures that an initialized array is always large enough to hold the initializers being specified. 10. A pointer is an object that contains a memory address. The pointer operators are & and *. 11. Yes, a pointer can be indexed like an array. Yes, an array can be accessed through a pointer. 12. // Count uppercase letters. 13. Multiple indirection is the term used for the situation in which one pointer points to another. 14. By convention, a null pointer is assumed to be unused. Module 5: Introducing Functions 1. The general form of a function is 2. 15. Yes, a function can return a pointer. No, a function cannot return an array. 16. // A custom version of strlen(). 5. No, a local variable’s value is lost when its function returns. (Or, more generally, its value is lost when its block is exited.) 6. The main advantages to global variables are that they are available to all other functions in the program and that they stay in existence during the entire lifetime of the program. Their main disadvantages are that they take up memory the entire time the program is executing, using a global where a local variable will do makes a function less general, and using a large number of global variables can lead to unanticipated side effects. 7. 8. 9. True. A prototype prevents a function from being called with the improper number of arguments. 10. Module 6: A Closer Look at Functions 1. An argument can be passed to a subroutine using call-by-value or call-by-reference. 2. A reference is an implicit pointer. A reference parameter is created by preceding the parameter name with an &. 3. f(ch, &i); 4. 5. 6. A function should not return a reference to a local variable, because that variable will go out-of-scope (that is, cease to exist) when the function returns. 7. Overloaded functions must differ in the type and/or number of their parameters. 8. C++ A Beginner’s Guide by Herbert Schildt 9. 10. Function overloading can introduce ambiguity when the compiler cannot decide which version of the function to call. This can occur when automatic type conversions are involved and when default arguments are used. Module 7: More Data Types and Operators 1. static int test = 100; 2. True. The volatile specifier tells the compiler that a variable might be changed by forces outside the program. 3. In a multifile project, to tell one file about a global variable declared in another file, use extern. 4. The most important attribute of a static local variable is that it holds its value between function calls. 5. // Use static to count function invocations. C++ A Beginner’s Guide by Herbert Schildt 20 6. Specifying x as register will have the most impact on performance, followed by y, and then z. The reason is that x is accessed most frequently within the loop, y the second most, and z is used only when the loop is initialized. 7. The & is a bitwise operator that acts on the individual bits within a value. && is a logical operator that acts on true/false values. 8. The statement multiplies the current value of x by 10 and assigns that result to x. It is the same as x = x * 10; 9. // Use rotations to encode a message. C++ A Beginner’s Guide by Herbert Schildt Module 8: Classes and Objects C++ A Beginner’s Guide by Herbert Schildt 1. A class is a logical construct that defines the form of an object. An object is an instance of a class. Thus, an object has physical reality within memory. 2. To define a class, use the class keyword. 3. Each object has its own copy of the member variables of a class. 4. 5. A constructor has the same name as its class. A destructor has the same name as its class except that it is preceded by a ~. 6. Here are three ways to create an object that initializes i to 10: 7. When a member function is declared within a class, it is automatically inlined, if possible. 8. // Create a Triangle class. 9. C++ A Beginner’s Guide by Herbert Schildt Module 9: A Closer Look at Classes 1. A copy constructor makes a copy of an object. It is called when one object initializes another. Here is the general form: 2. When an object is returned by a function, a temporary object is created as the return value. This object is destroyed by the object’s destructor after the value has been returned. 3. 4. A structure is a class in which members are public by default. A union is a class in which all data members share the same memory. Union members are also public by default. 5. *this refers to the object on which the function was called. 6. A friend function is a nonmember function that is granted access to the private members of the class for which it is a friend. 7. 8. To allow operations between a class type and a built-in type, you must use two friend operator functions, one with the class type as the first parameter, and one with the built-in type as the first parameter. 9. No, the ? cannot be overloaded. No, you cannot change the precedence of an operator. 10. C++ A Beginner’s Guide by Herbert Schildt 11. Module 10: Inheritance, Virtual Functions, and Polymorphism 1. A class that is inherited is called a base class. The class that does the inheriting is called a derived class. 2. A base class does not have access to the members of derived classes, because a base class has no knowledge of derived classes. A derived class does have access to the non-private members of its base class(es). 3. 4. To prevent a derived class from having access to a member of a base class, declare that member as private in the base class. 5. Here is the general form of a derived class constructor that calls a base class constructor: derived-class( ) : base-class( ) { // ... 6. Constructors are always called in order of derivation. Thus, when a Gamma object is created, the constructors are called in this order: Alpha, Beta, Gamma. 7. A protected member in a base class can be accessed by its own class and by derived classes. It is private, otherwise. 8. When a virtual function is called through a base class pointer, it is the type of the object being pointed to that determines which version of the function will be called. 9. A pure virtual function is a function that has no body inside its base class. Thus, a pure virtual function must be overridden by derived classes. An abstract class is a class that contains at least one pure virtual function. 10. No, an abstract class cannot be used to create an object. 11. A pure virtual function represents a generic description that all implementations of that function must adhere to. Thus, in the phrase “one interface, multiple methods,” the pure virtual function represents the interface, and the individual implementations represent the methods. Module 11: The C++ I/O System 1. The predefined streams are cin, cout, cerr, and clog. 2. Yes, C++ defines both 8-bit and wide-character streams. 3. The general form for overloading an inserter is shown here: 4. ios::scientific causes numeric output to be displayed in scientific notation. 5. The width( ) function sets the field width. 6. True, an I/O manipulator is used within an I/O expression. 7. Here is one way to open a file for text input: 8. Here is one way to open a file for text output: 9. ios::binary specifies that a file be opened for binary rather than text-based I/O. 10. True, at end-of-file, the stream variable will evaluate as false. 11. while(strm.get(ch)) // ... 12. There are many solutions. The following shows just one way: // Copy a file. 13. There are many solutions. The following shows one simple way: // Merge two files. 14. MyStrm.seekg(300, ios::beg); C++ A Beginner’s Guide by Herbert Schildt 35 Module 12: Exceptions, Templates, and Other Advanced Topics 1. C++ exception handling is built upon three keywords: try, catch, and throw. In the most general terms, program statements that you want to monitor for exceptions are contained in a try block. If an exception (that is, an error) occurs within the try block, it is thrown (using throw). The exception is caught, using catch, and processed. 2. When catching exceptions of both base and derived classes, the derived classes must precede the base class in a catch list. 3. void func() throw(MyExcpt) 4. Here is one way to add an exception to Queue. It is one of many solutions. C++ A Beginner’s Guide by Herbert Schildt 5. A generic function defines the general form of a routine, but does not specify the precise type of data upon which it operates. It is created using the keyword template. 6. Here is one way to make quicksort( ) and qs( ) into generic functions: 7. Here is one way to store Sample objects in a Queue: C++ A Beginner’s Guide by Herbert Schildt 40 8. Here, the Sample objects are allocated: C++ A Beginner’s Guide by Herbert Schildt 9. To declare a namespace called RobotMotion use: 10. The C++ standard library is contained in the std namespace. 11. No, a static member function cannot access the non-static data of a class. 12. The typeid operator obtains the type of an object at runtime. 13. To determine the validity of a polymorphic cast at runtime, use dynamic_cast. 14. const_cast overrides const or volatile in a cast.
本文档为【ANSWERS】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_261589
暂无简介~
格式:doc
大小:5MB
软件:Word
页数:43
分类:互联网
上传时间:2011-11-03
浏览量:10