• C++学习day4


    作业:

    1> 思维导图

     2> 整理代码

    1. 拷贝赋值函数课上代码

    1. //拷贝赋值函数课上代码
    2. #include
    3. using namespace std;
    4. //创建类
    5. class Stu
    6. {
    7. private://私有的
    8. string name;
    9. int socer;
    10. int *age;//此处注意用到指针类型
    11. public://共有的
    12. //无参构造函数(系统默认设置)
    13. Stu()
    14. {
    15. cout << "Stu:: 无参构造函数 " << endl;
    16. }
    17. //有参构造函数(自定义,自定义后需要将无参构造函数显性)
    18. Stu(string n,int s,int a):name(n),socer(s),age(new int (a))//初始化列表
    19. {
    20. cout << "Stu:: 有参构造函数" << endl;
    21. }
    22. //拷贝构造函数
    23. Stu(const Stu &other):name(other.name),socer(other.socer),age(new int(*other.age))//此处注意*other.age:因为定义是指针类型所以要去指针指向的内容进行赋值
    24. {
    25. cout << "Stu:: 拷贝构造函数" << endl;
    26. }
    27. //拷贝赋值函数
    28. Stu &operator=(const Stu &other) //此处不能用初始化列表因为是赋值操作
    29. {
    30. if(this != &other)//给自己赋值多此一举
    31. {
    32. name=other.name;
    33. socer=other.socer;
    34. age = new int (*other.age);//深拷贝赋值因为有指针防止段错误。。。
    35. }
    36. cout << "Stu:: 拷贝赋值函数" << endl;
    37. //注意返回值是自身引用
    38. return *this;
    39. }
    40. //析构函数
    41. ~Stu()//会自动调用
    42. {
    43. cout << "Stu:: 析构函数" << endl;
    44. }
    45. };
    46. int main()
    47. {
    48. Stu s1;//无参构造函数
    49. Stu s2("王一博",100,23);//有参构造函数
    50. Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2
    51. s1=s3;//拷贝赋值
    52. return 0;
    53. }

     2.匿名对象(用来初始化有名对象)课上代码

    1. void fun(Stu g)//形参接收实参的值==》Stu g=Stu("lisa",120,25)
    2. {
    3. }
    4. int main()
    5. {
    6. Stu s1;//无参构造函数
    7. Stu s2("王一博",100,23);//有参构造函数
    8. Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2
    9. //用到匿名对象给有名对象初始化
    10. Stu s4=Stu("赵云",88,40);
    11. //给数组初始化
    12. Stu s[2]={Stu("哪吒",200,40),Stu("张飞",300,30)};//和c中初始化相似
    13. //匿名对象作为函数实参使用
    14. fun(Stu("lisa",120,25));
    15. s1=s3;//拷贝赋值
    16. return 0;
    17. }

     3.全局函数作友元,课上代码

    1. //友元上课代码(全局函数作友元)
    2. #include
    3. using namespace std;
    4. //封装 房间类
    5. class Room
    6. {
    7. friend void goodfriend(Room &r);//关键字:friend
    8. private://私有的
    9. string bedroom;//卧室
    10. public://共有的
    11. string sittingroom;//客厅
    12. public:
    13. Room()
    14. {
    15. //在类内赋值
    16. bedroom="卧室";
    17. sittingroom="客厅";
    18. }
    19. };
    20. //全局函数作友元
    21. void goodfriend(Room &r)
    22. {
    23. cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑
    24. cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
    25. }
    26. int main()
    27. {
    28. Room r;
    29. goodfriend(r);
    30. return 0;
    31. }

    4.成员函数作友元,课上代码

    1. //成员函数作友元
    2. #include
    3. using namespace std;
    4. class Room;//需要声明一下,以免下面用到系统不知道报错
    5. //封装好朋友类
    6. class goodfriend
    7. {
    8. private:
    9. Room *r;//用到指针是因为指针的大小是固定的,而变量的大小不明,系统无法分配空间
    10. public:
    11. goodfriend();//在类内声明
    12. void vist();//在类内声明
    13. };
    14. //封装 房间类
    15. class Room
    16. {/*
    17. friend void goodfriend(Room &r);//关键字:friend*/
    18. friend void goodfriend::vist();
    19. private://私有的
    20. string bedroom;//卧室
    21. public://共有的
    22. string sittingroom;//客厅
    23. public:
    24. Room()
    25. {
    26. //在类内赋值
    27. bedroom="卧室";
    28. sittingroom="客厅";
    29. }
    30. };
    31. //类外实现
    32. goodfriend::goodfriend()
    33. {
    34. r=new Room;//申请空间
    35. }
    36. void goodfriend::vist()
    37. {
    38. cout << "好朋友正在参观" << r->sittingroom << endl;//共有权限访问成功毋庸置疑
    39. cout << "好朋友正在参观" << r->bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
    40. //指针需要用->
    41. }
    42. 全局函数作友元
    43. //void goodfriend(Room &r)
    44. //{
    45. // cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑
    46. // cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
    47. //}
    48. int main()
    49. {
    50. Room r;
    51. //goodfriend(r);
    52. goodfriend g;
    53. g.vist();
    54. return 0;
    55. }

     5.课前热身

    1. int const a; //......
    2. int const *p; //......
    3. int * const p; //
    4. int const * const p; //
    5. const int fun() {} //该函数是返回一个常整型变量

     6.常成员函数&&非常成员函数

    1.非常对象可以调用常成员函数,也可以调用非常成员函数,优先调用非常成员函数

    2.常对象只能调用常成员函数,如果没有常成员函数,则报错

    1. #include
    2. using namespace std;
    3. class Stu
    4. {
    5. private:
    6. string name;//只对该成员变量在常成员函数中可以被修改mutable
    7. int id;
    8. public:
    9. Stu(){}
    10. Stu(string name,int id):name(name),id(id)
    11. {}
    12. //常成员函数
    13. void display()const //==》Stu const * const this;表示指针的值和指向都不可改
    14. {
    15. //this ->name ="li si";报错原因:因为被const修饰不可以更改
    16. cout << name << " " << id <
    17. }
    18. //非常成员函数
    19. void display()//==>Stu * const this;和常成员重载
    20. {
    21. this -> name ="lisi";
    22. cout << name << " " << id << endl;
    23. }
    24. };
    25. int main()
    26. {
    27. cout << "Hello World!" << endl;
    28. const Stu s1("zhangsan",1001);//常对象
    29. s1.display();//常对象只能调用常成员函数
    30. return 0;
    31. }

     7.成员函数来实现运算符重载

    1. //运算符重载
    2. #include
    3. using namespace std;
    4. //构造类
    5. class Club
    6. {
    7. private://私有的
    8. int age;
    9. int hight;
    10. public:
    11. //无参
    12. Club(){}
    13. Club(int a,int h):age(a),hight(h)
    14. {
    15. }
    16. //成员函数实现算数运算符重载 加法
    17. const Club operator+(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    18. {
    19. //内部运算
    20. Club temp;
    21. temp.age=age+R.age;
    22. temp.hight=hight+R.hight;
    23. return temp;
    24. }
    25. //成员函数实现算数运算符重载 减法
    26. const Club operator-(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    27. {
    28. //内部运算
    29. Club temp;
    30. temp.age=age-R.age;
    31. temp.hight=hight-R.hight;
    32. return temp;
    33. }
    34. //成员函数实现算数运算符重载 乘法
    35. const Club operator*(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    36. {
    37. //内部运算
    38. Club temp;
    39. temp.age=age*R.age;
    40. temp.hight=hight*R.hight;
    41. return temp;
    42. }
    43. //成员函数实现算数运算符重载 除法
    44. const Club operator/(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    45. {
    46. //内部运算
    47. Club temp;
    48. temp.age=age/R.age;
    49. temp.hight=hight/R.hight;
    50. return temp;
    51. }
    52. //成员函数实现算数运算符重载 取余
    53. const Club operator%(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    54. {
    55. //内部运算
    56. Club temp;
    57. temp.age=age%R.age;
    58. temp.hight=hight%R.hight;
    59. return temp;
    60. }
    61. 关系运算符//
    62. //成员函数实现关系运算符重载 >
    63. bool operator>(const Club &R)const
    64. {
    65. if(age>R.age && hight>R.hight)
    66. {
    67. return true;
    68. }
    69. else
    70. return false;
    71. }
    72. //成员函数实现关系运算符重载 <
    73. bool operator<(const Club &R)const
    74. {
    75. if(age
    76. {
    77. return true;
    78. }
    79. else
    80. return false;
    81. }
    82. //成员函数实现关系运算符重载 >=
    83. bool operator>=(const Club &R)const
    84. {
    85. if(age>=R.age && hight>=R.hight)
    86. {
    87. return true;
    88. }
    89. else
    90. return false;
    91. }
    92. //成员函数实现关系运算符重载 <=
    93. bool operator<=(const Club &R)const
    94. {
    95. if(age<=R.age && hight<=R.hight)
    96. {
    97. return true;
    98. }
    99. else
    100. return false;
    101. }
    102. //成员函数实现关系运算符重载 ==
    103. bool operator==(const Club &R)const
    104. {
    105. if(age==R.age && hight==R.hight)
    106. {
    107. return true;
    108. }
    109. else
    110. return false;
    111. }
    112. //成员函数实现关系运算符重载 >
    113. bool operator!=(const Club &R)const
    114. {
    115. if(age!=R.age && hight!=R.hight)
    116. {
    117. return true;
    118. }
    119. else
    120. return false;
    121. }
    122. //赋值运算符
    123. //成员函数实现赋值运算符重载 +=
    124. Club &operator+=(const Club &R)
    125. {
    126. age += R.age;
    127. hight += R.hight;
    128. return *this;//返回自身
    129. }
    130. //成员函数实现赋值运算符重载 -=
    131. Club &operator-=(const Club &R)
    132. {
    133. age -= R.age;
    134. hight -= R.hight;
    135. return *this;//返回自身
    136. }
    137. //成员函数实现赋值运算符重载 =
    138. Club &operator=(const Club &R)
    139. {
    140. age = R.age;
    141. hight = R.hight;
    142. return *this;//返回自身
    143. }
    144. //成员函数实现赋值运算符重载 *=
    145. Club &operator*=(const Club &R)
    146. {
    147. age *= R.age;
    148. hight *= R.hight;
    149. return *this;//返回自身
    150. }
    151. //成员函数实现赋值运算符重载 /=
    152. Club &operator/=(const Club &R)
    153. {
    154. age /= R.age;
    155. hight /= R.hight;
    156. return *this;//返回自身
    157. }
    158. //成员函数实现赋值运算符重载 %=
    159. Club &operator%=(const Club &R)
    160. {
    161. age %= R.age;
    162. hight %= R.hight;
    163. return *this;//返回自身
    164. }
    165. void show()
    166. {
    167. cout << "age= " << age << " " << "hight= " << hight << endl;
    168. }
    169. };
    170. int main()
    171. {
    172. Club c1(10,10);
    173. Club c2(10,10);
    174. Club c3=c1+c2;
    175. Club c4=c1-c2;
    176. Club c5=c1*c1;
    177. Club c6=c1/c2;
    178. Club c7=c1%c2;
    179. cout << "算数运算符" << endl;
    180. c3.show();
    181. c4.show();
    182. c5.show();
    183. c6.show();
    184. c7.show();
    185. cout << "关系运算符" << endl;
    186. if(c3!=c1)
    187. {
    188. if(c3>c1)
    189. {
    190. cout << "c3>c1 " << endl;
    191. }
    192. else if(c3
    193. {
    194. cout << "c3 << endl;
    195. }
    196. cout << "c3!=c1" << endl;
    197. }
    198. else if(c3 == c1)
    199. {
    200. if(c3>=c1)
    201. {
    202. cout << "c3>=c1" << endl;
    203. }
    204. else if(c3<=c1)
    205. {
    206. cout << "c3<=c1" << endl;
    207. }
    208. {
    209. cout << "c3==c1" << endl;
    210. }
    211. }
    212. cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错
    213. c3+=c2;
    214. c4-=c2;
    215. c5*=c1;
    216. c6/=c2;
    217. c7%=c2;
    218. c3.show();
    219. c4.show();
    220. c5.show();
    221. c6.show();
    222. c7.show();
    223. return 0;
    224. }

    效果图:

     8.全局函数实现运算符重载

    1. //运算符重载
    2. #include
    3. using namespace std;
    4. //构造类
    5. class Club
    6. {
    7. friend const Club operator+(const Club &L,const Club &R);
    8. friend const Club operator-(const Club &L,const Club &R);
    9. friend const Club operator*(const Club &L,const Club &R);
    10. friend const Club operator/(const Club &L,const Club &R);
    11. friend const Club operator%(const Club &L,const Club &R);
    12. friend bool operator>(const Club &L,const Club &R);
    13. friend bool operator<(const Club &L,const Club &R);
    14. friend bool operator<=(const Club &L,const Club &R);
    15. friend bool operator>=(const Club &L,const Club &R);
    16. friend bool operator==(const Club &L,const Club &R);
    17. friend bool operator!=(const Club &L,const Club &R);
    18. friend Club &operator+=( Club &L,const Club &R);
    19. friend Club &operator-=( Club &L,const Club &R);
    20. friend Club &operator*=( Club &L,const Club &R);
    21. friend Club &operator/=(Club &L,const Club &R);
    22. friend Club &operator%=( Club &L,const Club &R);
    23. private://私有的
    24. int age;
    25. int hight;
    26. public:
    27. //无参
    28. Club(){}
    29. Club(int a,int h):age(a),hight(h)
    30. {
    31. }
    32. void show()
    33. {
    34. cout << "age= " << age << " " << "hight= " << hight << endl;
    35. }
    36. };
    37. //成员函数实现算数运算符重载 加法
    38. const Club operator+(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
    39. {
    40. //内部运算
    41. Club temp;
    42. temp.age=L.age-R.age;
    43. temp.hight=L.hight-R.hight;
    44. return temp;
    45. }
    46. //成员函数实现算数运算符重载 减法
    47. const Club operator-(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
    48. {
    49. //内部运算
    50. Club temp;
    51. temp.age=L.age-R.age;
    52. temp.hight=L.hight-R.hight;
    53. return temp;
    54. }
    55. //成员函数实现算数运算符重载 乘法
    56. const Club operator*(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
    57. {
    58. //内部运算
    59. Club temp;
    60. temp.age=L.age*R.age;
    61. temp.hight=L.hight*R.hight;
    62. return temp;
    63. }
    64. //成员函数实现算数运算符重载 除法
    65. const Club operator/(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
    66. {
    67. //内部运算
    68. Club temp;
    69. temp.age=L.age/R.age;
    70. temp.hight=L.hight/R.hight;
    71. return temp;
    72. }
    73. //成员函数实现算数运算符重载 取余
    74. const Club operator%(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
    75. {
    76. //内部运算
    77. Club temp;
    78. temp.age=L.age%R.age;
    79. temp.hight=L.hight%R.hight;
    80. return temp;
    81. }
    82. 关系运算符//
    83. //成员函数实现关系运算符重载 >
    84. bool operator>(const Club &L,const Club &R)
    85. {
    86. if(L.age>R.age && L.hight>R.hight)
    87. {
    88. return true;
    89. }
    90. else
    91. return false;
    92. }
    93. //成员函数实现关系运算符重载 <
    94. bool operator<(const Club &L,const Club &R)
    95. {
    96. if(L.age>R.age && L.hight
    97. {
    98. return true;
    99. }
    100. else
    101. return false;
    102. }
    103. //成员函数实现关系运算符重载 >=
    104. bool operator>=(const Club &L,const Club &R)
    105. {
    106. if(L.age>=R.age && L.hight>R.hight)
    107. {
    108. return true;
    109. }
    110. else
    111. return false;
    112. }
    113. //成员函数实现关系运算符重载 <=
    114. bool operator<=(const Club &L,const Club &R)
    115. {
    116. if(L.age<=R.age && L.hight>R.hight)
    117. {
    118. return true;
    119. }
    120. else
    121. return false;
    122. }
    123. //成员函数实现关系运算符重载 ==
    124. bool operator==(const Club &L,const Club &R)
    125. {
    126. if(L.age==R.age && L.hight==R.hight)
    127. {
    128. return true;
    129. }
    130. else
    131. return false;
    132. }
    133. //成员函数实现关系运算符重载 !=
    134. bool operator!=(const Club &L,const Club &R)
    135. {
    136. if(L.age!=R.age && L.hight>R.hight)
    137. {
    138. return true;
    139. }
    140. else
    141. return false;
    142. }
    143. //赋值运算符
    144. //成员函数实现赋值运算符重载 +=
    145. Club &operator+=( Club &L,const Club &R)
    146. {
    147. L.age += R.age;
    148. L.hight += R.hight;
    149. return L;//返回自身
    150. }
    151. //成员函数实现赋值运算符重载 -=
    152. Club &operator-=( Club &L,const Club &R)
    153. {
    154. L.age -= R.age;
    155. L.hight -= R.hight;
    156. return L;//返回自身
    157. }
    158. //成员函数实现赋值运算符重载 *=
    159. Club &operator*=( Club &L,const Club &R)
    160. {
    161. L.age *= R.age;
    162. L.hight *= R.hight;
    163. return L;//返回自身
    164. }
    165. //成员函数实现赋值运算符重载 /=
    166. Club &operator/=( Club &L,const Club &R)
    167. {
    168. L.age /= R.age;
    169. L.hight /= R.hight;
    170. return L;//返回自身
    171. }
    172. //成员函数实现赋值运算符重载 %=
    173. Club &operator%=(Club &L,const Club &R)
    174. {
    175. L.age /= R.age;
    176. L.hight /= R.hight;
    177. return L;//返回自身
    178. }
    179. int main()
    180. {
    181. Club c1(10,10);
    182. Club c2(10,10);
    183. Club c3=c1+c2;
    184. Club c4=c1-c2;
    185. Club c5=c1*c1;
    186. Club c6=c1/c2;
    187. Club c7=c1%c2;
    188. cout << "算数运算符" << endl;
    189. c3.show();
    190. c4.show();
    191. c5.show();
    192. c6.show();
    193. c7.show();
    194. cout << "关系运算符" << endl;
    195. if(c3!=c1)
    196. {
    197. if(c3>c1)
    198. {
    199. cout << "c3>c1 " << endl;
    200. }
    201. else if(c3
    202. {
    203. cout << "c3 << endl;
    204. }
    205. cout << "c3!=c1" << endl;
    206. }
    207. else if(c3 == c1)
    208. {
    209. if(c3>=c1)
    210. {
    211. cout << "c3>=c1" << endl;
    212. }
    213. else if(c3<=c1)
    214. {
    215. cout << "c3<=c1" << endl;
    216. }
    217. {
    218. cout << "c3==c1" << endl;
    219. }
    220. }
    221. cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错
    222. c3+=c2;
    223. c4-=c2;
    224. c5*=c1;
    225. c6/=c2;
    226. c7%=c2;
    227. c3.show();
    228. c4.show();
    229. c5.show();
    230. c6.show();
    231. c7.show();
    232. return 0;
    233. }

  • 相关阅读:
    【高等数学基础进阶】导数与微分
    Java代码审计-SQL注入
    麦子-linux字符设备驱动初探
    go语言编译过程概述
    科研-聚苯乙烯-b-聚丙烯酸胶束血红蛋白组装/牛血清白蛋白聚苯乙烯纳米微球PS/BSA
    ​LeetCode解法汇总337. 打家劫舍 III
    【STM32】读写内部Flash初步使用
    使用Shiro实现权限验证
    会stm32有机会进大公司吗?
    linux操作系统有什么好处?
  • 原文地址:https://blog.csdn.net/m0_69894626/article/details/133753028