1、Text.cpp#include fstreamstringusing namespace std;class Text string text;public: Text() Text(const string& fname) ifstream ifs(fname.c_str(); string line; while (getline(ifs, line) text += line + n; string contents() return text;int main(int argc, char* argv) if (argc 1) Text t1; Text t2(argv1); cou
2、t t1 :n t1.contents() endl;t2 : t2.contents() /:When creating a Text object, the compiler guarantees that the text data member has its default constructor (string:string( ) executed before either Text constructor runs, hence the default Text constructor just builds an empty string. This program prin
3、ts an empty string for t1 followed by the contents of the file named in the first command-line argument. Note the use of string:c_str( ) in the second constructor. Thats because the ifstream constructor takes a char* argument, not a string.7-2Create a Message class with a constructor that takes a si
4、ngle string with a default value. Create a private member string, and in the constructor simply assign the argument string to your internal string. Create two overloaded member functions called print( ): one that takes no arguments and simply prints the message stored in the object, and one that tak
5、es a string argument, which it prints in addition to the internal message. Does it make sense to use this approach instead of the one used for the constructor?Message.cppclass Message string msg; Message(const string& s = MSG) : msg(s) void print() msg void print(const string& suffix) suffix int mai
6、n() Message m1; Message m2(Error); m1.print(); m2.print(); m1.print(hello m2.print(goodbye/* Output:MSGErrorMSG helloError goodbye*/Its usually more flexible to allow optional arguments in the call to print, since the text of a message is fixed when it is created. A common technique allows an option
7、al prefix for messages, as the following example illustrates.MessageWithPrefix.cpp s) : prefix) prefix Message m(This is a message m.print(); m.print(AttentionThis is a messageAttention: This is a message7-3Determine how to generate assembly output with your compiler, and run experiments to deduce t
8、he name-decoration scheme.(Left to the reader)7-4Create a class that contains four member functions, with 0, 1, 2, and 3 int arguments, respectively. Create a main( ) that makes an object of your class and calls each of the member functions. Now modify the class so it has instead a single member fun
9、ction with all the arguments defaulted. Does this change your main( )?Heres the first version:ManyArgs.cppclass ManyArgs void f() n void f(int i) i n void f(int i, int j) , j void f(int i, int j, int k) k ManyArgs a; a.f(); a.f(1); a.f(1, 2); a.f(1, 2, 3);11, 21, 2, 3Now compare the output above to
10、that from this default-argument version:DefaultArgs.cppclass DefaultArgs void f(int i = 0, int j = 0, int k = 0) DefaultArgs a;0, 0, 01, 0, 01, 2, 0Although its true that the operations in main( ) did not change, the respective outputs suggest when each feature is appropriate. Use default arguments
11、when there truly is a default value (like zero above). When you want no value at all in certain instances, then the functions are different enough that you need the overloads.7-5Create a function with two arguments and call it from main( ). Now make one of the arguments a “placeholder” (no identifie
12、r) and see if your call in main( ) changes.NamelessArg.cppvoid twoArgs(int i, float x) twoArgs( x )nvoid placeHolder(int i, float) ,)n twoArgs(1, 2); placeHolder(1, 2);twoArgs(1, 2)twoArgs(1,)Placeholders are useful in those rare occasions (often in maintaining code) when you need different versions
13、 of a function, but only the type, not the value, of the differentiating parameter is important.(Exercises 6 10 left to the reader)7-6Modify Stash3.h and Stash3.cpp to use default arguments in the constructor. Test the constructor by making two different versions of a Stash object.7-7Create a new ve
14、rsion of the Stack class (from Chapter 6) that contains the default constructor as before, and a second constructor that takes as its arguments an array of pointers to objects and the size of that array. This constructor should move through the array and push each pointer onto the Stack. Test your c
15、lass with an array of string.7-8Modify SuperVar so that there are #ifdefs around all the vartype code as described in the section on enum. Make vartype a regular and publicenumeration (with no instance) and modify print( ) so that it requires a vartype argument to tell it what to do.7-9Implement Mem
16、2.h and make sure that the modified class still works with MemTest.cpp.7-10Use class Mem to implement Stash. Note that because the implementation is private and thus hidden from the client programmer, the test code does not need to be modified.In class Mem, add a bool moved( ) member function that t
17、akes the result of a call to pointer( ) and tells you whether the pointer has moved (due to reallocation). Write a main( ) that tests your moved( ) member function. Does it make more sense to use something like moved( ) or to simply call pointer( ) every time you need to access the memory in Mem?Last Update:06/27/2002
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2