为构建清朗、和谐、绿色、健康的网络环境,维护网络文明秩序,保障用户合法权益,菜鸟资源平台依据并贯彻《中华人民共和国民法典》、《中华人民共和国网络安全法》、《中华人民共和国个人信息保护法》、《中华人民共和国未成年人保护法》、《中华人民共和国预防未成年人犯罪法》、《网络信息内容生态治理规定》等相关法律法规及主管部门的管理政策,与用户共同制定《菜鸟资源平台自律公约》(以下简称“本公约”)。 立即查看
通知图标

正在访问 第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

接着上一章节继续:第六章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人 – 菜鸟资源 (xiciw.com) 4.9. 案例系统 string 与 MyString 4.9.1. string的使用 4.9.2. My...

当前版本

软件大小

软件语言

是否破解

第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

接着上一章节继续:第六章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人 – 菜鸟资源 (xiciw.com)

4.9. 案例系统 string MyString

4.9.1. string的使用

  1. int main()
  2. {
  3. // string s("china");
  4. string s = "china";
  5. string s2(s);
  6. string s3 = s2;
  7. string s4;
  8. s4 = s3;
  9. }

4.9.2. MyString 声明

  1. #ifndef MYSTRING_H_
  2. #define MYSTRING_H_
  3. #include <stddef.h>
  4. #include <iostream>
  5. class MyString {
  6. public:
  7. MyString(const char *str=NULL);
  8. MyString(const MyString & other);
  9. MyString & operator=(const MyString & another);
  10. MyString operator+(const MyString & other);
  11. bool operator==(const MyString &other);
  12. bool operator>(const MyString &other);
  13. bool operator<(const MyString &other);
  14. char& operator[](int idx);
  15. void dis();
  16. virtual ~MyString();
  17. private:
  18. char * _str;
  19. };
  20. #endif /* MYSTRING_H_ */

4.9.3. 构造

  1. MyString::MyString(const char *str) {
  2. if(str == NULL)
  3. {
  4. _str = new char[1];
  5. *_str = '�';
  6. }
  7. else
  8. {
  9. int len = strlen(str);
  10. _str = new char[len+1];
  11. strcpy(_str,str);
  12. }
  13. }

4.9.4. 析构

  1. MyString::~MyString() {
  2. delete []_str;
  3. }

4.9.5. 拷贝构造(深拷贝)

  1. MyString::MyString(const MyString & other)
  2. {
  3. int len = strlen(other._str);
  4. this->_str = new char[len+1];
  5. strcpy(this->_str,other._str);
  6. }

4.9.6. 赋值运算符重载

  1. MyString & MyString::operator=(const MyString & another)
  2. {
  3. if(this == &another)
  4. return *this;
  5. else
  6. {
  7. delete []this->_str;
  8. int len = strlen(another._str);
  9. this->_str = new char[len+1];
  10. strcpy(this->_str,another._str);
  11. return *this;
  12. }
  13. }

4.9.7. 加法运算符重载

  1. MyString MyString::operator+(const MyString & other)
  2. {
  3. int len = strlen(this->_str) + strlen(other._str);
  4. MyString str;
  5. delete []str._str;
  6. str._str = new char[len+1];
  7. memset(str._str,0,len+1);
  8. strcat(str._str,this->_str);
  9. strcat(str._str,other._str);
  10. return str;
  11. }

4.9.8. 关系运算符重载

  1. bool MyString::operator==(const MyString &other)
  2. {
  3. if(strcmp(this->_str,other._str) == 0)
  4. return true;
  5. else
  6. return false;
  7. }
  8. bool MyString::operator>(const MyString &other)
  9. {
  10. if(strcmp(this->_str,other._str) > 0)
  11. return true;
  12. else
  13. return false;
  14. }
  15. bool MyString::operator<(const MyString &other)
  16. {
  17. if(strcmp(this->_str,other._str) < 0)
  18. return true;
  19. else
  20. return false;
  21. }

4.9.9. []运算符重载

  1. char& MyString::operator[](int idx)
  2. {
  3. return _str[idx];
  4. }

4.9.10. 测试

  1. #include <iostream>
  2. #include "mystring.h"
  3. using namespace std;
  4. int main()
  5. {
  6. // MyString s = "china";
  7. MyString s("china"); //构造器
  8. s.dis();
  9. // MyString s2 = s;
  10. MyString s2(s); //拷贝构造器
  11. s2.dis();
  12. MyString s3;
  13. s3 = s2 = s; //赋值运算符重载
  14. s3.dis();
  15. MyString s4;
  16. s4 = "america"; //"america" 无名对象的构造器,赋值运算符重载
  17. return 0;
  18. }

4.10.课常练习

4.10.1. 实现钟表类

属性:时,分,秒

行为:run() 在屏幕上实现电子时钟 13:34:45 每隔一秒更新一个显示。

4.10.2. 分析:

构造时,初始化为当前系统时间,然后每隔一妙,刷屏。

4.10.3. 代码

  1. #include <iostream>
  2. #include <time.h>
  3. #include <unistd.h>
  4. using namespace std;
  5. class Clock
  6. {
  7. public:
  8. Clock()
  9. {
  10. time_t t = time(NULL);
  11. tm local = * localtime(&t);
  12. _hour = local.tm_hour;
  13. _minute = local.tm_min;
  14. _second = local.tm_sec;
  15. }
  16. void run()
  17. {
  18. for(;;)
  19. {
  20. tick();
  21. show();
  22. }
  23. }
  24. private:
  25. void tick()
  26. {
  27. sleep(1);
  28. if(++_second == 60)
  29. {
  30. _second = 0;
  31. if(++_minute == 60)
  32. {
  33. _minute = 0;
  34. if(++_hour == 24)
  35. {
  36. _hour = 0;
  37. }
  38. }
  39. }
  40. }
  41. void show()
  42. {
  43. system("cls");
  44. cout<<_hour<<":"<<_minute<<":"<<_second<<endl;
  45. }
  46. int _hour;
  47. int _minute;
  48. int _second;
  49. };
  50. int main()
  51. {
  52. Clock c;
  53. c.run();
  54. return 0;
  55. }

4.11.栈和堆上的对象及对象数组

4.11.1. 引例

  1. #include <iostream>
  2. using namespace std;
  3. class Stu
  4. {
  5. public:
  6. Stu(string n):_name(n){}
  7. void dis()
  8. {
  9. cout<<_name<<endl;
  10. }
  11. private:
  12. string _name;
  13. };
  14. int main()
  15. {
  16. // Stu s; //没有无参构造器
  17. // Stu s[5]= {Stu("zhangsan"),Stu("lisi")}; 不能指定个数,或部分初始化,则会报错。
  18. Stu s[]= {Stu("zhangsan"),Stu("lisi")};
  19. // Stu * ps = new Stu[4]{Stu("zhangsan")};
  20. // C11 中支持此种初始化方法,但必须对指名的类个数初始化,否则会报错。
  21. Stu * ps = new Stu[1]{Stu("zhangsan")};
  22. return 0;
  23. }

4.11.2. new delete生成销毁堆对象

new 一个堆对象,会自动调用构造函数,delete 一个堆对象对自动调用析构函数。这同 c 中 malloc 和 free 不同的地方。

4.11.3. 栈对象数组

如果生成的数组,未初始化,则必调用无参构造器。或手动调用带参构造器。

4.11.4. 堆对象数组

如果生成的数组,未初始化,则必调用无参构造器。或手动调用带参构造器。

4.11.5. 结论

构造器无论是重载还是默认参数,一定要把系统默认的无参构造器包含进来。不然生成数组的时候,可能会有些麻烦。

4.12.成员函数的存储方式

4.12.1. 类成员可能的组成

用类去定义对象时,系统会为每一个对象分配存储空间。如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间。

按理说,如果用同一个类定义了 10 个对象,那么就需要分别为 10 个对象的数据和函数代码分配存储单元。

第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

4.12.2. 类成员实际的组成

能否只用一段空间来存放这个共同的函数代码段,在调用各对象的函数时,都去调用这个公用的函数代码。

第七章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

显然,这样做会大大节约存储空间。C++编译系统正是这样做的,因此每个对象所占用的存储空间只是该对象的数据部分所占用的存储空间,而不包括函数代码所占用的存储空间。

  1. class Time
  2. {
  3. public:
  4. void dis()
  5. {
  6. cout<<hour<<minute<<sec<<endl;
  7. }
  8. private:
  9. int hour;
  10. int minute;
  11. int sec;
  12. };
  13. int main()
  14. {
  15. cout<<sizeof(Time)<<endl; //12
  16. //一个对象所占的空间大小只取决于该对象中数据成员所占的空间,而与成员函数无关
  17. return 0;
  18. }

4.12.3. 调用原理

所的有对象都调用共用的函数代码段,如何区分的呢,执行不同的代码可能有不用的结果。原因,C++设置了 this 指针,this 指针指向调用该函数的不同对象。当 t 调用dis()函数时,this 指向 t。当 t1 调用 dis()函数时,this 指向 t1;

  1. class Time
  2. {
  3. public:
  4. Time(int h, int m,int s)
  5. :hour(h),minute(m),sec(s){}
  6. void dis() //void dis(Time *p)
  7. {
  8. cout<<"this="<<this<<endl;
  9. cout<<this->hour<<":"<<this->minute<<":"<<this->sec<<endl;
  10. }
  11. private:
  12. int hour;
  13. int minute;
  14. int sec;
  15. };
  16. int main()
  17. {
  18. Time t(1,2,3);
  19. Time t2(2,3,4);
  20. t.dis(); //等价于 t.dis(&t)
  21. t2.dis();
  22. return 0;
  23. }

4.12.4. 注意事项

1,不论成员函数在类内定义还是在类外定义,成员函数的代码段都用同一种方式 存储。

2,不要将成员函数的这种存储方式和 inline(内置)函数的概念混淆。inline 的逻辑 意义是将函数内嵌到调用代码处,减少压栈与出栈的开支。

3,应当说明,常说的“某某对象的成员函数”,是从逻辑的角度而言的,而成员 函数的存储方式,是从物理的角度而言的,二者是不矛盾的。类似于二维数组是逻 辑概念,而物理存储是线性概念一样。

4.13.const 修饰符

4.13.1. 常数据成员

const 修饰类的成员变量,表示成员常量,不能被修改,同时它只能在初始化列表 中赋值。

可被 const 和非 const 成员函数调用,而不可以修改。

  1. class A
  2. {
  3. public:
  4. A():iValue(199){}
  5. private:
  6. const int iValue;
  7. };

4.13.2. 常成员函数

4.13.2.1. const 修饰函数的意义

承诺在本函数内部不会修改类内的数据成员,不会调用其它非 const 成员函数

4.13.2.2. const 修饰函数位置

const 修饰函数放在,声明这后,实现体之前,大概也没有别的地方可以放了。

  1. void dis() const
  2. {}

4.13.2.3. const 构成函数重载

  1. class A
  2. {
  3. public:
  4. A():x(199),y(299){}
  5. void dis() const //const 对象调用时,优先调用
  6. {
  7. //input(); 不能调用 非 const 函数,因为本函数不会修改,无法保证所调的函数也不会修改
  8. cout<<"x "<<x<<endl;
  9. cout<<"y "<<y<<endl;
  10. //y =200; const 修饰函数表示承诺不对数据成员修改。
  11. }
  12. void dis() //此时构成重载,非 const 对象时,优先调用。
  13. {
  14. y = 200;
  15. input();
  16. cout<<"x "<<x<<endl;
  17. cout<<"y "<<y<<endl;
  18. }
  19. void input()
  20. {
  21. cin>>y;
  22. }
  23. private:
  24. const int x;
  25. int y;
  26. };
  27. int main()
  28. {
  29. A a;
  30. a.dis();
  31. // const A a;
  32. // a.dis();
  33. return 0;
  34. }

小结:

1,如果 const 构成函数重载,const 对象只能调用 const 函数,非 const 对象优先 调用非 const 函数。

2,const 函数只能调用 const 函数。非 const 函数可以调用 const 函数。

3,类体外定义的 const 成员函数,在定义和声明处都需要 const 修饰符。

4.13.3. 常对象

  1. const A a;
  2. a.dis();

小结:

1,const 对象,只能调用 const 成员函数。

2,可访问 const 或非 const 数据成员,不能修改。

4.14.static 修饰符

在 C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用。所以在所有对象中都可以共享它。使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏(相比全局变量的优点)的原则,保证了安全性还可以节省内存。

类的静态成员,属于类,也属于对象,但终归属于类。

4.14.1. 类静态数据成员的定义及初始化

4.14.1.1. 声明:

  1. static 数据类型 成员变量; //在类的内部

4.14.1.2. 初始化

  1. 数据类型 类名::静态数据成员 = 初值; //在类的外部

4.14.1.3. 调用

  1. 类名::静态数据成员
  2. 类对象.静态数据成员

4.14.1.4. 案例

中国校园设计的“一塔湖图”

  1. #include <iostream>
  2. using namespace std;
  3. class School
  4. {
  5. public:
  6. static void addLibBooks(string book)
  7. {
  8. lib += book;
  9. }
  10. public:
  11. string tower;
  12. string lake;
  13. static string lib;
  14. };
  15. //类外实始化
  16. string School::lib = "Beijing lib:";
  17. int main()
  18. {
  19. School a,b,c,d;
  20. //static 数据成员,属于类,并不属于对象。
  21. //存储于 data 区的 rw 段
  22. cout<<sizeof(a)<<sizeof(b)<<sizeof(c)<<sizeof(d)<<endl;
  23. //类的方式访问,编译有问题,必须要初始化。
  24. //在无对象生成的时候,亦可以访问。
  25. School::lib = "China lib:";
  26. cout<<School::lib<<endl;
  27. //lib 虽然属于类,但是目的是为了实现类对象间的共享
  28. //故对象也是可以访问的。
  29. cout<<a.lib<<endl;
  30. cout<<b.lib<<endl;
  31. //为了搞好图书馆的建设,提设 static 接口
  32. School::addLibBooks("mao xuan");
  33. cout<<School::lib<<endl;
  34. return 0;
  35. }

4.14.1.5. 小结

1,static 成员变量实现了同类对象间信息共享。

2,static 成员类外存储,求类大小,并不包含在内。

3,static 成员是命名空间属于类的全局变量,存储在 data 区。

4,static 成员使用时必须实始化,且只能类外初始化。

5,可以通过类名访问(无对象生成时亦可),也可以通过对象访问。

4.14.2. 类静态成员函数的定义

为了管理静态成员,c++提供了静态函数,以对外提供接口。并静态函数只能访问静态成员。

4.14.2.1. 声明

  1. static 函数声明

4.14.2.2. 调用

  1. 类名::函数调用
  2. 类对象.函数调用

4.14.2.3. 案例

  1. #include <iostream>
  2. using namespace std;
  3. class Student
  4. {
  5. public:
  6. Student(int n,int a,float s):num(n),age(a),score(s){}
  7. void total()
  8. {
  9. count++;
  10. sum += score;
  11. }
  12. static float average();
  13. private:
  14. int num;
  15. int age;
  16. float score;
  17. static float sum;
  18. static int count;
  19. };
  20. float Student::sum = 0;
  21. int Student::count = 0;
  22. float Student::average()
  23. {
  24. return sum/count;
  25. }
  26. int main()
  27. {
  28. Student stu[3]= {
  29. Student(1001,14,70),
  30. Student(1002,15,34),
  31. Student(1003,16,90)
  32. };
  33. for(int i=0; i<3; i++)
  34. {
  35. stu[i].total();
  36. }
  37. cout<<Student::average()<<endl;
  38. r eturn 0;
  39. }

4.14.2.4. 小结

1,静态成员函数的意义,不在于信息共享,数据沟通,而在于管理静态数据成员,完成对静态数据成员的封装。

2,静态成员函数只能访问静态数据成员。原因:非静态成员函数,在调用时 this 指针时被当作参数传进。而静态成员函数属于类,而不属于对象,没有 this 指针

4.14.3. 综合案例
4.14.3.1. 单例模式
4.14.3.2. cocos 渲染树的模拟
每生成一个节点,自动挂到静态表头上去。

  1. #include <string.h>
  2. using namespace std;
  3. class Student
  4. {
  5. public:
  6. Student(string n)
  7. :name(n)
  8. {
  9. if(head == NULL)
  10. {
  11. head = this;
  12. this->next = NULL;
  13. }
  14. else{
  15. this->next = head;
  16. head= this;
  17. }
  18. //可优化
  19. }
  20. static void printStudentList();
  21. static void deleteStudentList();
  22. private:
  23. string name;
  24. Student * next;
  25. static Student * head;
  26. };
  27. void Student::printStudentList()
  28. {
  29. Student * p = head;
  30. while(p != NULL)
  31. {
  32. cout<<p->name<<endl;
  33. p = p->next;
  34. }
  35. }
  36. void Student::deleteStudentList()
  37. {
  38. Student *p = head;
  39. while(head)
  40. {
  41. head = head->next;
  42. delete p;
  43. p = head;
  44. }
  45. }
  46. Student * Student::head = NULL;
  47. int main()
  48. {
  49. string name;
  50. string postName;
  51. char buf[1024];
  52. for(int i=0; i<10; i++)
  53. {
  54. name = "stu";
  55. postName = (itoa(i,buf,10));
  56. name += postName;
  57. new Student(name);
  58. }
  59. Student::printStudentList();
  60. Student::deleteStudentList();
  61. return 0;
  62. }

4.15.static const 成员在下一章才菜鸟资源精讲课程讲解

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

常见问题

  • 如何安装和激活?

    每款软件会附带有安装教程,您打开安装包一目了然

  • 程序是否已损坏?文件损坏?还是其他错误?

    有错误问题,请您参考:https://www.xxx.com/error

  • 如何更新?正式激活后会发生什么情况?

    除非安装说明中另有说明,否则官方更新可能会导致无法激活。 要从本网站更新软件,您需要在此处下载此软件的新版本(如果可用),并将其安装在计算机上安装的版本之上(替换)。在这种情况下,您将保存此软件的激活和设置。

  • 如何下载?链接不起作用?

    我们使用百度网盘,132云盘和微软网盘,除了百度网盘,其他两款不限速,如果链接失效,请您联系客服处理

  • 已发布更新。我什么时候升级我的版本?

    所有软件如有更新,我们第一时间推送,视自己情况更新使用

  • 如何更改语言?

    打开“系统偏好设置”->“通用>语言和地区”->应用程序-“+”。 选择应用和语言。此方法适用于大多数应用程序。 Adobe 产品中的语言通常是在产品本身的安装阶段选择的。 游戏中的语言通常会在游戏本身的设置中发生变化。

  • 如何删除软件?

    有很多选择。最简单的方法是使用特殊的实用程序来卸载应用程序,例如App Cleaner Uninstaller 要删除 Adobe 产品,请使用 Creative Cloud Cleaner Tool

  • 需要远程帮助吗?

    网站已开通永久会员的可享受免费远程,如果非永久会员,远程安装另外收费

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    技术教程

    第六章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

    2024-1-29 15:44:35

    技术教程

    第八章丨C++编程宝典:快速上手、深入进阶、挑战高级技巧,助你成为编程达人

    2024-1-30 15:27:27

    0 条回复 A文章作者 M管理员
    有些面具戴得太久,就摘不下来了
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    后退
    榜单