C++基础语法知识点归纳IV

继续我的《c++ Primer Plus 》的学习

Code:
  1. #include<iostream>   
  2. using namespace std;   
  3.   
  4. struct sysop   
  5. {   
  6.     char name[26];   
  7.     char quote[64];   
  8.     int used;   
  9. };   
  10.   
  11. const sysop &use(sysop &sysopref);   
  12. int main()   
  13. {   
  14.     // 将引用应用于结构 /   
  15.     sysop looper =    
  16.     {   
  17.         "Rick /"Fortran/" Looper",   
  18.             "I'm a goto kind of guy.",   
  19.             0   
  20.     };   
  21.   
  22.     use(looper);   
  23.     cout << "Looper: " << looper.used << " use(s)/n";   
  24.     sysop copycat;   
  25.     copycat = use(looper);   
  26.     cout << "Looper: " << looper.used << " use(s)/n";   
  27.     cout << "Copycat: " << copycat.used << " use(s)/n";   
  28.     cout << "use(looper): " << use(looper).used << " use(s)/n";   
  29.   
  30.     cout << endl << &looper <<" : " << ©cat << endl;   
  31.     return 0;   
  32. }   
  33.   
  34. const sysop& use(sysop& sysopref)    ///函数返回引用   
  35. {   
  36.     cout << sysopref.name << " says:/n";   
  37.     cout << sysopref.quote << endl;   
  38.     sysopref.used++;   
  39.     return sysopref;   
  40. }  
Code:
  1. #include<iostream>   
  2. #include<string>   
  3. using namespace std;   
  4.   
  5. string version1(const string & s1, const string & s2);   
  6. const string & version2(string & s1, const string & s2);   
  7. const string & version3(string & s1, const string & s2);   
  8.   
  9. /// 下面这个程序是关于引用知识点的,理解这个程序的用法,引用基本上就OK了//   
  10. int main()   
  11. {   
  12.     string input;   
  13.     string copy;   
  14.     string result;   
  15.   
  16.     cout << "Enter a string: ";   
  17.     getline(cin, input);   
  18.     copy = input;   
  19.     cout << "Your string as entered: " << input << endl;   
  20.     result = version1(input,"***");   
  21.     cout << "Your string enhanced: " << result << endl;   
  22.     cout << "Your original string: " << input << endl;   
  23.   
  24.     result = version2(input,"###");   
  25.     cout << "Your string enhanced: " << result << endl;   
  26.     cout << "Your original string: " << input << endl;   
  27.   
  28.     cout << "Resetting original string./n";   
  29.     input = copy;   
  30.     result = version3(input,"@@@");   
  31.     cout << "Your string enhanced: " << result << endl;   
  32.     cout << "Your original string: " << input << endl;   
  33.   
  34.     return 0;   
  35. }   
  36.   
  37. string version1(const string & s1, const string & s2)   
  38. {   
  39.     string temp;   
  40.   
  41.     temp = s2 + s1 + s2;   
  42.     return temp;   
  43. }   
  44.   
  45. const string & version2(string & s1, const string & s2)   
  46. {   
  47.     s1 = s2 + s1 + s2;   
  48.     return s1;   
  49. }   
  50.   
  51. const string & version3(string & s1, const string & s2)  //这种返回局部变量的方法是错误的,编译时会有警告,在运行时会让系统崩溃   
  52. {   
  53.     string temp;   
  54.   
  55.     temp = s2 + s1 + s2;   
  56.     return temp;   
  57. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值