8.1 Questions
1#include <iostream>2 voidSillyConst Char* S,intn =0);3 intMain ()4 {5 Char*pointer ="Lala";6Silly"Hello?");7 for(inti =0; I <5; i++)8 Silly (pointer,i);9 }Ten voidSillyConst Char* S,intN) One { A Static intUse =0; - intLimit = + +Use ; - for(inti =0; i < limit; i++) theStd::cout << S <<'\ t'<<"limit ="<< Limit <<'\ t'<<"i ="<< I <<Std::endl; -}
Stastic restricting local variables
8.4 Questions
1#include <iostream>2#include <cstring>//for strlen (), strcpy ()3 using namespacestd;4 structstringy {5 Char* STR;//points to a string6 intCt//length of string (not counting ')7 };8 voidShowConst Char*STR,intCNT =1);9 voidShowConstStringy & BNY,intCNT =1);Ten void Set(Stringy & BNY,Const Char*str); One intMainvoid) A { - stringy Beany; - CharTesting[] ="Reality isn ' t what it used to be."; the Set(Beany, testing);//First argument is a reference, - //allocates space to hold copy of testing, - //sets STR member of Beany to the - //new block, copies testing to new block, + //and sets CT member of Beany -Show (Beany);//Prints member string once +Show (Beany,2);//prints member string twice Atesting[0] ='D'; attesting[1] ='u'; -Show (testing);//Prints testing string once -Show (Testing,3);//Prints testing string thrice -Show"done!"); - return 0; - } in //corresponding show (testing) - voidShowConst Char*STR,intCNT) to { + while(cnt-->0) -cout << str <<Endl; the } * //corresponding Show (Beany) $ voidShowConstStringy &bny,intCNT)Panax Notoginseng { - while(cnt-->0) thecout << bny.str <<Endl; + } A void Set(Stringy & BNY,Const Char*str) the { +Bny.ct =strlen (str); -Bny.str =New Char[Bny.ct +1]; $ strcpy (BNY.STR,STR); $ }
The function prototype CNT = 1, if the default does not enter CNT, then the default is 1; If you enter a value of n, you get the new value N. For example, 20 and a line of code.
8.5 questions
1#include <iostream>2Template <classT>3 t Max5 (t ar[])4 {5 intN;6T max = ar[0];7 for(n =0; N <5; n++)8 if(Ar[n] >max)9Max =Ar[n];Ten returnMax; One } A Const intLIMIT =5; - intMain () - { the DoubleArr1[limit] = { A,32.23,-423,-37.987,35.9}; - intArr2[limit] = { the, -, the, $,- -}; -Std::cout << max5 (arr1) <<Std::endl; -Std::cout << max5 (arr2) <<Std::endl; + } -
function template.
Eighth chapter: function Exploration