The eighth chapter: function exploration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.