C++ Primer Plus (P73) potx

20 263 0
C++ Primer Plus (P73) potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

.2:Suppose the song() function has this prototype: void song(char * name, int times); How would you modify the prototype so that the default value for times is 1? a. What changes would you make in the function definition? b. Can you provide a default value of "O, My Papa" for name? c. A: void song(char * name, int times = 1);a. None. Only prototypes contain the default value information. b. Yes, providing you retain the default value for times: void song(char * name = "O, My Papa", int times = 1); c. .3:Write overloaded versions of iquote(), a function that displays its argument enclosed in double quotation marks. Write three versions: one for an int argument, one for a double argument, and one for a string argument. A:You can use either the string "\"" or the character '"' to print a quotation mark. The following functions show both methods. #include <iostream.h> void iquote(int n) { cout << "\"" << n << "\""; } void iquote(double x) { cout << '"' << x << '"'; } This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. void iquote(const char * str) { cout << "\"" << str << "\""; } .4:Here is a structure template: A: This function shouldn't alter the structure members, so use the const qualifier. void show_box(const box & container) { cout << "Made by " << container. maker << "\n"; cout << "Height = " << container.height << "\n"; cout << "Width = " << container.width << "\n"; cout << "Length = " << container.length << "\n"; cout << "Volume = " << container.volume << "\n"; } a. void set_volume(box & crate) { crate.volume = crate.height * crate.width * crate.length; } b. .5:Here are some desired effects. Indicate whether each can be accomplished with default arguments, function overloading, both, or neither. Provide appropriate prototypes. mass(density, volume) returns the mass of an object having a density of density and a volume of volume, whereas mass(density) returns the mass having a density of density and a volume of 1.0 cubic meters. All quantities are type double. a. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. repeat(10, "I'm OK") displays the indicated string ten times, whereas repeat("But you're kind of stupid") displays the indicated string five times. b. average(3,6) returns the int average of two int arguments, whereas average(3.0, 6.0) returns the double average of two double values. c. mangle("I'm glad to meet you") returns the character I or a pointer to the string "I'm mad to gleet you" depending on whether you assign the return value to a char variable or to a char* variable. d. A: This can be done using a default value for the second argument: double mass(double d, double v = 1.0); It can also be done by overloading: double mass(double d, double v); double mass(double d); a. You can't use a default for the repeat value because you have to provide default values from right to left. You can use overloading: void repeat(int times, const char * str); void repeat(const char * str); b. You can use function overloading: int average(int a, int b); double average(double x, double y); c. You can't do this one because both versions would have the same signature. d. .6:Write a function template that returns the larger of its two arguments. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. A: template<class T> T max(T t1, T t2) // or T max(const T & t1, const T & t2) { return t1 > t2? t1 : t2; } .7:Given the template of Review Question 6 and the box structure of Review Question 4, provide a template specialization that takes two box arguments and returns the one with the larger volume. A: template<> box max(box b1, box b2) { return b1.volume > b2.volume? b1 : b2; } Chapter 9 .1:What storage scheme would you use for the following situations? homer is a formal argument (parameter) to a function. a. The secret variable is to be shared by two files. b. The topsecret variable is to be shared by the functions in one file but hidden from other files. c. beencalled keeps track of how many times the function containing it has been called. d. A: homer is automatically an automatic variable. a. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. secret should be defined as an external variable in one file and declared using extern in the second file. b. topsecret could be defined as a static variable with internal linkage by prefacing the external definition with the keyword static. Or it could be defined in an unnamed namespace. c. beencalled should be defined as a local static variable by prefacing a declaration in the function with the keyword static. d. .2:Discuss the differences between a using-declaration and a using-directive. A:A using declaration makes a single name from a namespace available, and it has the scope corresponding to the declarative region in which the using declaration occurs. A using directive makes all the names in a namespace available. When you use a using directive, it is as if you declared the names in the smallest declarative region containing both the using declaration and the namespace itself. .3:Rewrite the following so that it doesn't use using-declarations or using-directives. #include <iostream> using namespace std; int main() { double x; cout << "Enter value: "; while (! (cin >> x) ) { cout << "Bad input. Please enter a number: "; cin.clear(); while (cin.get() != '\n') continue; } cout << "Value = " << x << endl; This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. return 0; } A: #include <iostream> using namespace std; int main() { double x; std::cout << "Enter value: "; while (! (std::cin >> x) ) { std::cout << "Bad input. Please enter a number: "; std::cin.clear(); while (std::cin.get() != '\n') continue; } std::cout << "Value = " << x << std::endl; return 0; } .4:Rewrite the following so that it uses using-declarations instead of the using-directive. #include <iostream> using namespace std; int main() { double x; cout << "Enter value: "; while (! (cin >> x) ) { cout << "Bad input. Please enter a number: "; cin.clear(); while (cin.get() != '\n') This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. continue; } cout << "Value = " << x << endl; return 0; } A:Rewrite the following so that it uses using declarations instead of the using directive. #include <iostream> int main() { using std::cin; using std::cout; using std::endl; double x; cout << "Enter value: "; while (! (cin >> x) ) { cout << "Bad input. Please enter a number: "; cin.clear(); while (cin.get() != '\n') continue; } cout << "Value = " << x << endl; return 0; } .5:The average(3,6) function returns an int average of the two int arguments when called in one file, and it returns a double average of the two int arguments when called in a second file in the same program. How could you set this up? A:You could have separate static function definitions in each file. Or each file could define the appropriate average() function in an unnamed namespace. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. .6:What will the following two-file program display? // file1.cpp #include <iostream> using namespace std; void other(); void another(); int x = 10; int y; int main() { cout << x << endl; { int x = 4; cout << x << endl; cout << y << endl; } other(); another(); return 0; } void other() { int y = 1; cout << "Other: " << x << "," << y << endl; } // file 2.cpp #include <iostream> using namespace std; extern int x; namespace { int y = -4; } void another() This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. { cout << "another(): " << x << ", " << y << endl; } A: 10 4 0 Other: 10, 1 another(): 10, -4 .7:What will the following program display? #include <iostream> using namespace std; void other(); namespace n1 { int x = 1; } namespace n2 { int x = 2; } int main() { using namespace n1; cout << x << endl; { int x = 4; cout << x << ", " << n1::x << ", " << n2::x << endl; } using n2::x; cout << x << endl; other(); This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. return 0; } void other() { using namespace n2; cout << x << endl; { int x = 4; cout << x << ", " << n1::x << ", " << n2::x << endl; } using n2::x; cout << x << endl; } A: 1 4, 1, 2 2 2 4, 1, 2 2 Chapter 10 .1:What is a class? A:A class is a definition of a user-defined type. A class declaration specifies how data is to be stored, and it specifies the methods (class member functions) that can be used to access and manipulate that data. .2:How does a class accomplish abstraction, encapsulation, and data hiding? A:A class represents the operations one can perform on a class object with a public interface of class methods; this is abstraction. The class can use private visibility (the This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [...]... provide them explicitly? Describe how these implicitly generated functions behave This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks A: C++ automatically provides the following member functions: A default constructor if you define no constructors A copy constructor if you don't define one An assignment operator if you don't define one . created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. A :C++ automatically provides the following member functions: A default constructor if you define no

Ngày đăng: 07/07/2014, 06:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan