p425.1

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6. class Cow{
  7. char name[];
  8. char *hobby;
  9. double weight;
  10. public:
  11. Cow();
  12. Cow(const char *nm, const char *ho, double wt);
  13. Cow(const Cow &c);
  14. ~Cow();
  15. Cow &operator=(const Cow &c);
  16. void ShowCow()const;
  17. };
  18.  
  19. Cow::Cow(){
  20. strcpy(name, "no body");
  21. hobby = "nothing";
  22. weight = 0.0;
  23. }
  24.  
  25. Cow::Cow(const char *nm, const char *ho, double wt){
  26. std::strncpy(name, nm, );
  27. hobby = new char[strlen(ho) + ];
  28. strcpy(hobby, ho);
  29. weight = wt;
  30. }
  31.  
  32. Cow::Cow(const Cow &c){
  33. hobby = new char[strlen(c.hobby) + ];
  34. strcpy(hobby, c.hobby);
  35. strcpy(name, c.name);
  36. weight = c.weight;
  37. }
  38.  
  39. Cow::~Cow(){
  40. delete[]hobby;
  41. }
  42.  
  43. Cow & Cow::operator=(const Cow &c){
  44. if (this == &c)
  45. return *this;
  46. delete[]hobby;
  47. hobby = new char[strlen(c.hobby) + ];
  48. strcpy(hobby, c.hobby);
  49. strcpy(name, c.name);
  50. weight = c.weight;
  51. cout << "hobby " << hobby << endl
  52. << "name " << name << endl
  53. << "weight " << weight << endl;
  54. return *this;
  55. }
  56.  
  57. void Cow::ShowCow()const{
  58. std::cout << "name is " << name << std::endl
  59. << "hobby is " << hobby << std::endl
  60. << "weight is " << weight << std::endl;
  61. }
  62.  
  63. int main(){
  64. Cow test1, test2("Max", "soccer", 6.7);
  65. test1.ShowCow();
  66. test2.ShowCow();
  67. Cow test3("Stack", "vollyball", 3.45);
  68. cin.get();
  69. test1 = test3;
  70. test1.ShowCow();
  71.  
  72. system("pause");
  73. return ;
  74. }

p426.3

  1. //头文件:
  2. #include<iostream>
  3. #include<string>
  4. using std::istream;
  5. using std::ostream;
  6.  
  7. #ifndef STRING2_H_
  8. #define STRING2_H_
  9. class String{
  10. private:
  11. char *str;
  12. int len;
  13. static int num_strings;
  14. static const int CINIM = ;
  15. public:
  16. String(const char *s);
  17. String();
  18. String(const String &);
  19. ~String();
  20. int length()const { return len; }
  21. String & operator=(const String &);
  22. String &operator=(const char*);
  23. char&operator[](int i);
  24. const char &operator[](int i)const;
  25. String & Stringlow();
  26. char * Stringup();
  27. int has(char);
  28. friend char * operator+(const String &st1, const String &st2);
  29. friend bool operator<(const String &st1, const String &st2);
  30. friend bool operator==(const String &st1, const String &st2);
  31. friend ostream &operator<<(ostream &os, const String &st);
  32. friend istream &operator>>(istream &is, String &st);
  33. static int howmany();
  34. };
  35.  
  36. #endif
  37.  
  38. //方法:
  39. #include<iostream>
  40. #include<cctype>
  41. #include<cstring>
  42. #include<string>
  43. #include"String2.h"
  44.  
  45. using std::cin;
  46. using std::cout;
  47. using std::endl;
  48.  
  49. int String::num_strings = ;
  50.  
  51. String::String(const char *s){
  52. num_strings++;
  53. len = strlen(s);
  54. str = new char[len+];
  55. strcpy(str, s);
  56. cout << "num_strings " << num_strings << endl;
  57. }
  58.  
  59. String::String(){
  60. num_strings++;
  61. len = ;
  62. str = NULL;
  63. cout << "num_strings " << num_strings << endl;
  64. }
  65.  
  66. String::String(const String &st){
  67. num_strings++;
  68. len = st.len;
  69. str = new char[len + ];
  70. strcpy(str, st.str);
  71. cout << "num_strings " << num_strings << endl;
  72. }
  73.  
  74. String::~String(){
  75. num_strings--;
  76. delete[]str;
  77. cout << "num_strings " << num_strings << endl;
  78. }
  79.  
  80. String & String::operator=(const String &st){
  81. if (&st == this)
  82. return *this;
  83. delete[]str;
  84. len = st.len;
  85. str = new char[len + ];
  86. strcpy(str, st.str);
  87. return *this;
  88. }
  89.  
  90. String & String::operator=(const char*s){
  91. delete[]str;
  92. len = strlen(s);
  93. str = new char[len + ];
  94. strcpy(str, s);
  95. return *this;
  96. }
  97.  
  98. char & String::operator[](int i){
  99. return str[i];
  100. }
  101.  
  102. const char & String::operator[](int i)const{
  103. return str[i];
  104. }
  105.  
  106. String & String::Stringlow(){
  107. for (int i = ; i < len; i++)
  108. str[i] = tolower(str[i]);
  109. return *this;
  110. }
  111.  
  112. char * String::Stringup(){
  113. for (int i = ; i < len; i++)
  114. str[i] = toupper(str[i]);
  115. return str;
  116. }
  117.  
  118. int String::has(char ch){
  119. int count = ;
  120. for (int i = ; i < len; i++)
  121. if (str[i] == ch)
  122. count++;
  123. return count;
  124. }
  125.  
  126. char * operator+(const String &st1, const String &st2){
  127. char *st3 = new char[st1.len + st2.len+];
  128. for (int i = ; i < st1.len; i++)
  129. st3[i] = st1[i];
  130. for (int j = ; j < st2.len; j++)
  131. st3[st1.len ++ j] = st2[j];
  132. st3[st1.len] = ' ';
  133. st3[st1.len + st2.len + ] = '\0';
  134. return st3;
  135. }
  136.  
  137. bool operator<(const String &st1, const String &st2){
  138. if (strcmp(st1.str, st2.str))
  139. return false;
  140. else return true;
  141. }
  142.  
  143. bool operator==(const String &st1, const String &st2){
  144. if (strcmp(st1.str, st2.str) == )
  145. return true;
  146. else return false;
  147. }
  148.  
  149. ostream &operator<<(ostream &os, const String &st){
  150. os << "str: " << st.str << endl;
  151. return os;
  152. }
  153.  
  154. istream &operator>>(istream &is, String &st){
  155. char temp[String::CINIM];
  156. is.get(temp, String::CINIM);
  157. if (is)
  158. st = temp;
  159. while (is&&is.get() != '\n')
  160. continue;
  161. return is;
  162. }
  163.  
  164. int String::howmany(){
  165. return num_strings;
  166. }
  167.  
  168. //驱动:
  169. #include<iostream>
  170. #include<cstdlib>
  171. using namespace std;
  172. #include "string2.h"
  173.  
  174. int main(){
  175. String s1(" and i am a C++ student. ");
  176. String s2 = "please enter your name: ";
  177. String s3;
  178. cout << s2;
  179. cin >> s3;
  180. s2 = "my name is " + s3;
  181. cout << s2 << ".\n";
  182. s2 = s2 + s1;
  183. s2.Stringup();
  184. cout << "the string\n" << s2 << "\ncontains " <<
  185. s2.has('A') << "'A' characters in it.\n";
  186. s1 = "red";
  187. String rgb[] = { String(s1), String(" green"), String("blue") };
  188. cout << "enter the name of a primary color for mixing light: ";
  189. String ans;
  190. bool success = false;
  191. while (cin >> ans){
  192. ans.Stringlow();
  193. for (int i = ; i < ; i++){
  194. if (ans == rgb[i]){
  195. cout << "that's right!\n";
  196. success = true;
  197. break;
  198. }
  199. }
  200. if (success)
  201. break;
  202. else
  203. cout << "try again\n";
  204. }
  205. cout << "bye\n";
  206. system("pause");
  207. return ;
  208. }

c++ primer plus 习题答案(6)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  4. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. Unix/Linux环境C编程入门教程(8) FreeBSD CCPP开发环境搭建

    1. FreeBSD是一种自由类Unix操作系统,是由经过BSD.386BSD和4.4BSD发展而来的类Unix的一个重要分支.FreeBSD拥有超过200名活跃开发者和上千名贡献者.FreeBSD被 ...

  2. Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 1.广义的Debian是指一个致力于创建自由操作系统的合作组织及其作品,由于Deb ...

  3. window.open打开新页面,并将本页数据用过url传递到打开的页面;需要两个页面;

    页面1 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...

  4. 加入收藏夹的js代码(求兼容chrome浏览器的代码)

    从网上找了加入收藏夹的js代码,但不兼容chrome,不知道有没有兼容chrome的相关代码,希望有知道的告诉一下,谢谢! 代码如下 $("#id").click(function ...

  5. NYOJ 7-街区最短路径问题(曼哈顿距离)

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...

  6. Android版xx助手之天天酷跑外挂具体分析

    Android版xx助手之天天酷跑外挂具体分析 图/文      莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也開 ...

  7. 利用jquery写的一个TAB页切换效果

    函数如下 /** *切换效果 */ function switab(tab,con,tab_c_css,tab_n_css,no) { $(tab).each(function(i){ if(i == ...

  8. English - refer to...和refer to...as

    refer to...和refer to...as...本来就是refer的两个固定搭配,这个只能讲讲后两者用法,剩下的就是单独的refer的用法了. 1. refer to sb/sth 指的是/提 ...

  9. 2014.9.3数据库CRUD

    CRUD 增删改查 DCL 数据控制语言:备份,grant DML 数据操作语言: CRUD DDL 数据定义语言:create drop alter 自增长列不能赋值 增: Insert into  ...

  10. VS2013 快捷键乱掉如何修改回来

    比如 CTRL+E+C =注释 F6=重新生成解决方案 CTRL+D+Q=运行时快速监视 工具-->选项-->环境-->键盘-->应用以下其他键盘映射方案,下拉选择 Visua ...