C++ 脑图(一)根本、类与对象、内存管理、继续

[复制链接]
发表于 2025-11-15 14:22:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次

1、C++根本
IO流
namespace
引用、const
inline、函数参数
重载
2、类和对象
类举例
3、 内存管理
new/delete
对象内存分布
内存走漏
4、继续
继续权限
继续中的构造与析构
菱形继续


1、C++根本


IO流

  1. #include <iostream>
  2. #include <iomanip> // 对于setprecision, setw等
  3. #include <string>
  4. #include <fstream> // 对于文件操作
  5. using namespace std;
  6. int main()
  7. {
  8.   int num;
  9.   cin >> num; // 输入流
  10.   if (cin.fail())
  11.   {
  12.     cerr << "input not right"; // 标准err输出
  13.   }
  14.   cout << num << endl; // 标准输出
  15.   // 二进制文件读写
  16.   ofstream binfile("test.bin", ofstream::out | ofstream::binary);
  17.   char in[] = "A";
  18.   binfile.write(in, 1);
  19.   binfile.put('B');
  20.   binfile.close();
  21.   ifstream ifile("test.bin", ofstream::in | ofstream::binary); // 直接构造也可以
  22.   ifile.seekg(0, ifile.end);                                   // 跳转到文件末尾
  23.   int length = ifile.tellg();                                  // 获取当前字符在文件当中的位置,即文件的字符总数
  24.   ifile.seekg(0, ifile.beg);                                   // 重新回到文件开头
  25.   char data[1024];
  26.   ifile.read(data, length); // 将文件当中的数据全部读取到字符串data当中
  27.   cout << data << endl;
  28.   ifile.close(); // 关闭文件
  29.   ofstream outFile("example.txt"); // 只读,创建 ofstream 对象
  30.   if (!outFile)
  31.   {
  32.     std::cerr << "Unable to open file for writing";
  33.     return 1; // 返回错误代码
  34.   }
  35.   outFile << "hello" << endl;
  36.   outFile << "test" << endl;
  37.   outFile.close();
  38.   ifstream input("example.txt"); // 只读
  39.   if (!input)
  40.   {
  41.     cerr << "Unable to open file";
  42.     return 1;
  43.   }
  44.   std::string line;
  45.   while (std::getline(input, line))
  46.   {
  47.     std::cout << line << std::endl;
  48.   }
  49.   input.close(); // 关闭文件
  50.   fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app);
  51.   file << "\nAppending this line to the file." << std::endl;
  52.   file.seekg(0, std::ios::beg);
  53.   // 从文件读取数据并输出到控制台
  54.   std::string line1;
  55.   while (std::getline(file, line1))
  56.   {
  57.     std::cout << line1 << std::endl;
  58.   }
  59.   file.close(); // 关闭文件
  60.   return 0;
  61. }
复制代码
std::endl 等效于插入换行符后调用std::cout.flush(),每次革新都会欺凌将缓冲区数据写入到输出装备,但是大概会触发体系调用,用户态和内核空间的切换带来额外开销影响性能。
namespace

  1. #include <iostream>
  2. using namespace std;
  3. namespace A
  4. {
  5.     int a=1;
  6. } // namespace A
  7. namespace B
  8. {
  9.     int a=2;
  10.     namespace C
  11.     {
  12.         struct T
  13.         {
  14.             int a=4;
  15.             char c;
  16.         };
  17.         int a=3;
  18.     } // namespace C
  19. }; // namespace B
  20. int main()
  21. {
  22.     cout<<"A::a "<<A::a<<"; B::a "<<B::a<<"; C::a "<<B::C::a<<endl;
  23.     B::C::T t1;
  24.     cout<<t1.a<<endl;
  25.     using B::C::T;
  26.     T t2;
  27.     cout<<t2.a<<endl;
  28.     bool F=true;
  29.     cout<<boolalpha<<F<<endl; //noboolalpha 输出1
  30.     int a=1,b=2,c=3;
  31.     auto max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);//三目运算
  32.     (a<b? a : b)= 30;//C中的三目运算符返回的是变量值,不能作为左值使用.而C++则可以在任意地方
  33.     return 0;
  34. }
复制代码
引用、const

  1. #include <iostream>
  2. using namespace std;
  3. struct A
  4. {
  5.     int a;
  6.     char c;
  7. };
  8. void show(A *p)
  9. {
  10.     cout << "show func 指针调用前" << endl;
  11.     cout << p->a << "\t" << p->c << endl;
  12.     p->a = 20;
  13.     cout << "show fun 指针修改调用后" << endl;
  14.     cout << p->a << "\t" << p->c << endl;
  15. }
  16. void show1(A &p)
  17. {
  18.     cout << "show1 引用调用前" << endl;
  19.     cout << p.a << "\t" << p.c << endl;
  20.     p.a = 35;
  21.     cout << "show1 引用修改调用后" << endl;
  22.     cout << p.a << "\t" << p.c << endl;
  23. }
  24. void show2(A p)
  25. { // 临时变量实参的一份拷贝,修改对实参没有影响
  26.     cout << "show2 传值调用前" << endl;
  27.     cout << p.a << "\t" << p.c << endl;
  28.     p.a = 45;
  29.     cout << "show2 传值修改调用后" << endl;
  30.     cout << p.a << "\t" << p.c << endl;
  31. }
  32. // 指针所指向的内存空间,不能被修改
  33. int operA(const A *p)
  34. {
  35.     // p->a=10;
  36.     return 0;
  37. }
  38. // 指针变量本身不能被修改
  39. int operatorA1(A *const pT)
  40. {
  41.     pT->a = 10;
  42.     // pT = NULL; //
  43.     return 0;
  44. }
  45. // 指针变量和所指的值都不可以改变
  46. int operatorA2(const A *const pT)
  47. {
  48.     // pT->a = 10;
  49.     // pT = NULL; //
  50.     cout << "A!" << endl;
  51.     return 0;
  52. }
  53. // 数据交换, 值交换无法完成
  54. void myswap(int i, int j)
  55. {
  56.     int c = 0;
  57.     c = i;
  58.     i = j;
  59.     j = c;
  60. }
  61. // 数据交换, 指针直接对实参修改
  62. void myswap1(int *i, int *j)
  63. {
  64.     int c = 0;
  65.     c = *i;
  66.     *i = *j;
  67.     *j = c;
  68. }
  69. // 数据交换, 引用交换,引用做函数参数时不用初始化
  70. void myswap2(int &i, int &j)
  71. {
  72.     int c = 0;
  73.     c = i;
  74.     i = j;
  75.     j = c;
  76. }
  77. // 返回a的本身 a的副本
  78. int getA1()
  79. {
  80.     int a = 10;
  81.     return a;
  82. }
  83. /*想让引用返回可以用就需要将变量生命为static的
  84. /*当函数返回值为引用时若返回栈变量不能成为其它引用的初始值
  85. 不能作为左值使用若返回静态变量或全局变量可以成为其他引用的初始值
  86. 即可作为右值使用,也可作为左值使用*/
  87. int &getA2()
  88. {
  89.     int a = 20;
  90.     // warning: reference to local variable ‘a’ returned [-Wreturn-local-addr]
  91.     return a;
  92. }
  93. int *getA3()
  94. {
  95.     int a = 30;
  96.     // warning: reference to local variable ‘a’ returned
  97.     return &a;
  98. }
  99. int main()
  100. {
  101.     // const  operatorA*
  102.     const int x = 0;
  103.     int const y = 2;
  104.     // x=3;   y=2;//两个定义一样的效果,修改报错assignment of read-only variable 'x'
  105.     // int const z; //未被初始化 error: uninitialized 'const z
  106.     // 引用
  107.     int var = 10;
  108.     int &temp = var;                               // 引用需要初始化,像一个常量,有自己的空间sizeof与指针一样
  109.     temp = 12;                                     // 变量的别名
  110.     cout << "var: " << var << " " << temp << endl; // 修改引用同修改本身 12 12
  111.     int temp1 = 10, temp2 = 110;
  112.     cout << "berfor myswap temp1: " << temp1 << " temp2: " << temp2 << endl;
  113.     myswap(temp1, temp2); // 数据交换, 值交换无法完成
  114.     cout << "after myswap temp1: " << temp1 << " temp2: " << temp2 << endl;
  115.     myswap1(&temp1, &temp2); // 指针传入交换
  116.     cout << "after myswap temp1: " << temp1 << " temp2: " << temp2 << endl;
  117.     myswap2(temp1, temp2); // 引用的本质时间接修改实参,编译器创造了指针然后修改
  118.                            // 间接赋值的三个条件,1)定义两个变量,2)变量之间建立关联,3)形参间接修改实参;引用是后两个条件合二为一
  119.     cout << "after myswap temp1: " << temp1 << " temp2: " << temp2 << endl;
  120.     cout << "   ----复杂类型引用 学习---- " << endl;
  121.     A t = {101, 'A'}; // 结构体 列表初始化
  122.     cout << "main调用指针调用前" << endl;
  123.     cout << t.a << "\t" << t.c << endl; // 101 A
  124.     show(&t);
  125.     cout << "main调用指针调用后" << endl;
  126.     cout << t.a << "\t" << t.c << endl;
  127.     show1(t);
  128.     cout << "main引用调用后" << endl;
  129.     cout << t.a << "\t" << t.c << endl;
  130.     show2(t);
  131.     cout << "main传值调用后" << endl;
  132.     cout << t.a << "\t" << t.c << endl;
  133.     cout << "------函数返回值是一个引用-------" << endl;
  134.     int ga = 1;
  135.     int ga1 = 2;
  136.     ga = getA1();
  137.     // ga1=getA2(); //引用相当于指针,临时变量返回易出错。因为值被释放了
  138.     // int &ga2=getA2(); //栈变量返回不能作为初始值
  139.     cout << "ga: " << ga << endl;
  140.     int *p = NULL; // 指针可以为null
  141.     // p = getA3();
  142.     // cout << *p << endl; // 证明了引用和指针一样,返回临时变量的指针,有可能被释放了再去使用出现异常
  143.     return 0;
  144. }
复制代码
 C++有了指针为什么还要引入引用,明显指针可以办理这些标题?
引入引用的直接缘故原由是提供一种更安全、更简便的间接访问机制
  1. void func(const complex* x,complex* y){
  2.     complex z=*x+*y;  //丑陋,有时需要判空
  3. }
  4. void func1(const complex& x, complex& y){
  5.     complex z=x+y;
  6. }
复制代码
inline、函数参数

  1. #include <iostream>
  2. using namespace std;
  3. #define MYFUNC(a,b)((a)<(b)?(a):(b))
  4. inline void printA()
  5. {
  6.     int a = 10;
  7.     cout << "a:" << a << endl;
  8. }
  9. inline int Myfun(int a,int b){
  10.     return a<b?a:b;
  11. }
  12. struct A
  13. {
  14.     char c;
  15.     int a;
  16. };
  17. void show(const A &m)
  18. {
  19.     // m.c=10; // error: assignment of member 'A::c' in read-only object
  20.     cout << m.a << endl;
  21. }
  22. //函数占位参数 函数调用时,必须写够参数
  23. void func1(int a,int b, int){
  24.     cout<<"a: "<<a<<" b: "<<b<<endl;
  25. }
  26. //默认参数与占位参数
  27. void func2(int a,int b, int=0){
  28.     cout<<"a: "<<a<<" b: "<<b<<endl;
  29. }
  30. // void func3(int a=0,int b){ ////在默认参数规则 ,如果默认参数出现,那么右边的都必须有默认参数
  31. // }
  32. int main()
  33. {
  34.     int a = 10;
  35.     int &b = a; // 普通引用必须初始化
  36.     // int &c=10; //字面值不可以,引用要取地址
  37.     int x = 12;
  38.     const int &y = x; // 让变量引用只读属性不能通过y修改x
  39.     // y=20; 不能修改, x可以改变
  40.     x = 13;  //修改值y也变了
  41.     // 常引用的初始化可以用变量或者常量值
  42.     {
  43.         const int a = 10;
  44.         // int &m=11;
  45.         const int &m = 40;
  46.     }
  47.     A t = {'1', 1};
  48.     show(t);
  49.     cout<<" ----inline--------"<<endl;
  50.     printA();//与下相同
  51.     //         {
  52.     //                 int a = 10;
  53.     //                 cout<<"a"<<a<<endl;
  54.     //         }
  55.     ////C++编译器直接将函数体插入在函数调用的地方
  56.     //内联函数省去了普通函数调用时压栈,跳转和返回的开销
  57.     int a1=1, b1=3;
  58.     int c=MYFUNC(a1,b1);
  59.     int d=Myfun(a1,b1);
  60.     cout<<" define: a1: "<<a1<<" b1: "<<b1<<" c: "<<c<<endl;
  61.     cout<<" inlice: a1: "<<a1<<" b1: "<<b1<<" d: "<<d<<endl;
  62.     //int c1=MYFUNC(++a1,b1); //a=3 b=3 c=3
  63.     //==>宏替换并展开 ((++a) < (b) ? (++a) : (b))  //a=3 b=3 c=3
  64.    // cout<<" define ++: a1: "<<a1<<" b1: "<<b1<<" c1: "<<c1<<endl;
  65.    int d1=Myfun(++a1,b1);  //a=2 b=3 c=2
  66.     cout<<" inlice ++: a1: "<<a1<<" b1: "<<b1<<" d: "<<d1<<endl;
  67.     func1(a,b,c); //占位符必须有参数
  68.     func2(a,b); //ok
  69.     func2(a,b,c); //ok
  70.     return 0;
  71. }
复制代码
重载

  1. #include <iostream>
  2. using namespace std;
  3. int func(int x)
  4. {
  5.     return x;
  6. }
  7. int func(int x, int y)
  8. {
  9.     return x + y;
  10. }
  11. // 函数重载  和  函数默认参数 在一起
  12. // int func(int x, int y, int c = 0)
  13. // {
  14. //     return x + y + c;
  15. // }
  16. auto func(const string &s)
  17. {
  18.     return s.size();
  19. }
  20. // void func(int x,int y){ //函数返回值不是函数重载的判断标准
  21. //     x= x+y;
  22. //     cout<<x<<endl;
  23. // }
  24. void func(string *s1, string *s2)
  25. {
  26.     cout << *s1 << "\t" << *s2 << endl;
  27. }
  28. // 定义一个函数类型
  29. typedef int(myfun)(int a, int b);
  30. myfun *f = nullptr; // 定义函数指针,指向函数入口
  31. // 声明一个函数指针类型
  32. typedef int (*myfunc1)(int a, int b);
  33. //myfunc1 fp = nullptr;
  34. // 定义一个函数指针 变量
  35. void (*myVarPFunc)(int a, int b);
  36. int main()
  37. {
  38.     int a = 0;
  39.     a = func(1);
  40.     cout << "func a: " << a << endl;
  41.     // a=func(1,2);  //重载与默认参数在一起产生了二义性
  42.     cout << "func1 a: " << a << endl;
  43.     cout << "func2 a: " << func("abcd") << endl;
  44.     string s = "abcde";
  45.     string s1 = "hjic";
  46.     func(&s, &s1);
  47.     myfunc1 fp;
  48.     fp = func;
  49.     cout<<fp(1,3)<<endl;
  50.     return 0;
  51. }
复制代码
2、类和对象


类举例

  1. #include <iostream>
  2. #include <string>
  3. class Student {
  4. private:
  5.     std::string name; // 学生姓名
  6.     int age;          // 学生年龄
  7. public:
  8.     // 默认构造函数
  9.     Student() : name("Unknown"), age(0) {
  10.         std::cout << "Default constructor called.\n";
  11.     }
  12.     // 带参数的构造函数
  13.     Student(const std::string& name, int age) : name(name), age(age) {
  14.         std::cout << "Parameterized constructor called for " << name << ".\n";
  15.     }
  16.     // 拷贝构造函数
  17.     Student(const Student& other) {
  18.         this->name = other.name; // 使用 this 指针
  19.         this->age = other.age;   // 使用 this 指针
  20.         std::cout << "Copy constructor called for " << this->name << ".\n";
  21.     }
  22.     // 拷贝赋值运算符
  23.     Student& operator=(const Student& other) {
  24.         if (this != &other) { // 防止自我赋值
  25.             this->name = other.name; // 使用 this 指针
  26.             this->age = other.age;   // 使用 this 指针
  27.             std::cout << "Copy assignment operator called for " << this->name << ".\n";
  28.         }
  29.         return *this; // 返回当前对象的引用
  30.     }
  31.     // 析构函数
  32.     ~Student() {
  33.         std::cout << "Destructor called for " << name << ".\n";
  34.     }
  35.     // 显示学生信息
  36.     void display() const {
  37.         std::cout << "Name: " << name << ", Age: " << age << "\n";
  38.     }
  39. };
  40. int main() {
  41.     // 使用默认构造函数
  42.     Student student1;
  43.     student1.display();
  44.     // 使用带参数的构造函数
  45.     Student student2("Alice", 20);
  46.     student2.display();
  47.     // 使用拷贝构造函数
  48.     Student student3 = student2; // 调用拷贝构造函数
  49.     student3.display();
  50.     // 使用拷贝赋值运算符
  51.     Student student4;
  52.     student4 = student2; // 调用拷贝赋值运算符
  53.     student4.display();
  54.     return 0;
  55. }
复制代码
3、 内存管理


new/delete

  1. #include <iostream>
  2. #include <cstdlib> // 包含 malloc 和 free 的头文件
  3. class MyClass {
  4. public:
  5.     MyClass() {
  6.         std::cout << "MyClass constructor called.\n";
  7.     }
  8.     ~MyClass() {
  9.         std::cout << "MyClass destructor called.\n";
  10.     }
  11. };
  12. int main() {
  13.     // 使用 new 和 delete
  14.     std::cout << "Using new and delete:\n";
  15.     MyClass* obj1 = new MyClass(); // 使用 new 分配内存
  16.     delete obj1; // 使用 delete 释放内存
  17.     // 使用 malloc 和 free
  18.     std::cout << "\nUsing malloc and free:\n";
  19.     // 注意:malloc 只分配内存,不调用构造函数
  20.     MyClass* obj2 = (MyClass*)malloc(sizeof(MyClass)); // 使用 malloc 分配内存
  21.     if (obj2) {
  22.         new (obj2) MyClass(); // 手动调用构造函数
  23.     }
  24.     // 使用 free 释放内存,但需要手动调用析构函数
  25.     if (obj2) {
  26.         obj2->~MyClass(); // 手动调用析构函数
  27.         free(obj2); // 使用 free 释放内存
  28.     }
  29.     return 0;
  30. }
复制代码
对象内存分布

  1. #include <iostream>
  2. #include <string>
  3. class MyClass {
  4. public:
  5.     int value;
  6.     MyClass(int v) : value(v) {
  7.         std::cout << "constructor called for value: " << value << "\n"; //10
  8.     }
  9.     ~MyClass() {
  10.         std::cout << "destructor called for value: " << value << "\n";
  11.     }
  12. };
  13. // 全局变量,存储在数据区
  14. MyClass globalObj(10);
  15. int main() {
  16.     // 栈区对象
  17.     MyClass stackObj(20);
  18.     std::cout << "Address of stackObj: " << &stackObj << "\n";  //10
  19.     // 堆区对象
  20.     MyClass* heapObj = new MyClass(30);
  21.     std::cout << "Address of heapObj: " << heapObj << "\n";
  22.     // 数据区对象
  23.     std::cout << "Address of globalObj: " << &globalObj << "\n";
  24.     // 程序区(代码区)对象
  25.     std::cout << "Address of main function: " << (void*)main << "\n";
  26.     // 释放堆区对象
  27.     delete heapObj;
  28.     /*
  29.     constructor called for value: 10
  30.     constructor called for value: 20
  31.     Address of stackObj: 0x6963bffbd4
  32.     constructor called for value: 30
  33.     Address of heapObj: 0x22296241640
  34.     Address of globalObj: 0x7ff6cc537030
  35.     Address of main function: 0x7ff6cc531450
  36.     destructor called for value: 30
  37.     destructor called for value: 20
  38.     destructor called for value: 10
  39.     */
  40.     return 0;
  41. }
复制代码
内存走漏

  1. #include <iostream>
  2. class MyClass {
  3. public:
  4.     MyClass() {
  5.         std::cout << "MyClass constructor called.\n";
  6.     }
  7.     ~MyClass() {
  8.         std::cout << "MyClass destructor called.\n";
  9.     }
  10. };
  11. void createMemoryLeak() {
  12.     MyClass* obj = new MyClass(); // 动态分配内存
  13.     // 忘记释放内存,导致内存泄漏
  14. }
  15. void createNoMemoryLeak() {
  16.     std::unique_ptr<MyClass> obj = std::make_unique<MyClass>(); // 使用智能指针
  17.     // 不需要手动释放内存,智能指针会自动管理
  18. }
  19. int main() {
  20.     for (int i = 0; i < 5; ++i) {
  21.         createMemoryLeak(); // 每次调用都会造成内存泄漏
  22.     }
  23.     std::cout << "Memory leak has occurred.\n";
  24.     for (int i = 0; i < 5; ++i) {
  25.         createNoMemoryLeak(); // 不会造成内存泄漏
  26.     }
  27.     return 0;
  28. }
复制代码
4、继续


继续权限

  1. #include <iostream>
  2. using namespace std;
  3. // 继承
  4. // public 修饰的成员变量 方法 在类的内部 类的外部都能使用
  5. // protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ;其他 类的外部不能被使用
  6. // private: 修饰的成员变量方法 只能在类的内部使用 不能在类的外部
  7. // 这几个修饰符用在继承里影响的是子类的访问全限
  8. // 类型兼容性原则
  9. class Parent
  10. {
  11. public:
  12.    void show()
  13.    {
  14.       cout << " show 父类" << endl;
  15.       a = 0;
  16.       b = 1;
  17.       c = 2;
  18.       cout << "a: " << a << " b: " << b << " c: " << c << endl;
  19.    }
  20.    int a;
  21.    Parent()
  22.    {
  23.       cout << " --父类 无参 构造 --" << endl;
  24.    }
  25.    Parent(const Parent &p)
  26.    {
  27.       cout << " 父类 拷贝 构造--- " << endl;
  28.    }
  29. protected:
  30.    int b;
  31. private:
  32.    int c;
  33. };
  34. class Child1 : public Parent
  35. {
  36. private:
  37.    int c1; // 子类有自己的属性
  38. public:
  39.    int a1;
  40.    void show1()
  41.    { // 访问控制
  42.       a1 = 1;
  43.       b1 = 2;
  44.       c1 = 3;
  45.       cout << " show child1 子类1" << endl;
  46.       cout << a << b << a1 << b1 << c1 << endl;
  47.       //<< c  在子类范围内父类里的属性不变,c是私有已经是类外了
  48.    }
  49. protected:
  50.    int b1;
  51. };
  52. class Child2 : protected Parent
  53. {
  54. public:
  55.    int a2;
  56.    void show2()
  57.    { // 访问控制 protected继承,public变成protected,
  58.       a2 = 2;
  59.       b2 = 4;
  60.       c2 = 4;
  61.       cout << " child2 子类2" << endl;
  62.       cout << a << b << a2 << b2 << c2 << endl;
  63.       //<< c  在子类范围内父类里的属性不变,c是私有已经是类外了
  64.    }
  65. protected:
  66.    int b2;
  67. private:
  68.    int c2; // 子类有自己的属性
  69. public:
  70. };
  71. class Child3 : private Parent
  72. {
  73. public:
  74.    int a3;
  75.    void show3()
  76.    { // 访问控制 私有继承,全变私有,
  77.       a = 0;
  78.       b = 1;
  79.       a3 = 4;
  80.       b3 = 4;
  81.       c3 = 5;
  82.       cout << " child3 子类3" << endl;
  83.       cout << a << b << a3 << b3 << c3 << endl;
  84.       //<< c  在子类范围内父类里的属性不变,c是私有已经是类外了
  85.    }
  86. protected:
  87.    int b3;
  88. private:
  89.    int c3;
  90. };
  91. void howShow(Parent *base){ //指针
  92.    base->show();//父类成员函数
  93. }
  94. void howShow1(Parent &base){ //引用
  95.    base.show();//父类成员函数
  96. }
  97. int main()
  98. {
  99.    Child1 cl;
  100.    // 子类继承了父类的所有属性和方法
  101.    cout << " 公有继承 " << endl;
  102.    cl.a = 1; // public ,类外可以
  103.    // 全是类外不可以访问
  104.    //  cl.b=1;
  105.    //  cl.c=1;
  106.    //  cl.a1=2;
  107.    //  cl.b1=2;
  108.    //  cl.c1=3;
  109.    cl.show1();
  110.    cout << " 保护共有继承 " << endl;
  111.    Child2 c2;
  112.    // 子类继承了父类的所有属性和方法
  113.    // c2.a = 1; //变protected ,类外可以
  114.    // 全是类外不可以访问
  115.    // c2.b=1;
  116.    // c2.c=1;
  117.    // c2.a2=2;
  118.    // c2.b2=2;
  119.    // c2.c2=3;
  120.    c2.show2();
  121.    Child3 c3;
  122.    // 私有更不用说了
  123.    c3.show3();
  124.    Parent p1;
  125.    p1.show();
  126.    //基类指针指向子类
  127.    Parent *p=nullptr;
  128.    p=&cl;
  129.    p->show();
  130.    //指针做函数参数
  131.    howShow(p);
  132.    howShow(&cl);
  133.    //引用做函数参数
  134.    howShow1(p1);
  135.    howShow1(cl);
  136.    //第二层含义
  137.         //可以让子类对象   初始化   父类对象
  138.         //子类就是一种特殊的父类
  139.         Parent p3 = cl;
  140.    return 0;
  141. }
复制代码
继续中的构造与析构

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. /*
  5.     1、子类对象在创建时会首先调用父类的构造函数
  6.         2、父类构造函数执行结束后,执行子类的构造函数
  7.         3、当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
  8.         4、析构函数调用的先后顺序与构造函数相反
  9. */
  10. class Object
  11. {
  12. public:
  13.         Object(int a, int b)
  14.         {
  15.                 this->a = a;
  16.                 this->b = b;
  17.                 cout<<"object构造函数 执行 "<<"a"<<a<<" b "<<b<<endl;
  18.         }
  19.         ~Object()
  20.         {
  21.                 cout<<"object析构函数 \n";
  22.         }
  23. protected:
  24.         int a;
  25.         int b;
  26. };
  27. class Parent : public Object
  28. {
  29. public:
  30.         Parent(int p) : Object(1, 2)
  31.         {
  32.                 this->p = p;
  33.                 cout<<"父类构造函数..."<<p<<endl;
  34.         }
  35.         ~Parent()
  36.         {
  37.                 cout<<"析构函数..."<<p<<endl;
  38.         }
  39.         void printP(int a, int b)
  40.         {
  41.                 cout<<"我是爹..."<<endl;
  42.         }
  43. protected:
  44.         int p;
  45.        
  46. };
  47. class child : public Parent
  48. {
  49. public:
  50.         child(int p) : Parent(p) , obj1(3, 4), obj2(5, 6)
  51.         {
  52.                 this->p = p;
  53.                 cout<<"子类的构造函数"<<p<<endl;
  54.         }
  55.         ~child()
  56.         {
  57.                 cout<<"子类的析构"<<p<<endl;
  58.         }
  59.         void printC()
  60.         {
  61.                 cout<<"我是儿子"<<endl;
  62.         }
  63. protected:
  64.         int p;
  65.         Object obj1;
  66.         Object obj2;
  67. };
复制代码
菱形继续

  1. #include <iostream>
  2. // 基类
  3. class Base {
  4. public:
  5.     void show() {
  6.         std::cout << "Base class show function called.\n";
  7.     }
  8. };
  9. // 派生类 A
  10. class DerivedA : public Base {
  11. public:
  12.     void show() {
  13.         std::cout << "DerivedA class show function called.\n";
  14.     }
  15. };
  16. // 派生类 B
  17. class DerivedB : public Base {
  18. public:
  19.     void show() {
  20.         std::cout << "DerivedB class show function called.\n";
  21.     }
  22. };
  23. // 最终派生类 C
  24. class DerivedC : public DerivedA, public DerivedB {
  25. public:
  26.     void show() {
  27.         std::cout << "DerivedC class show function called.\n";
  28.     }
  29. };
  30. int main() {
  31.     DerivedC obj;
  32.     // 调用 DerivedC 的 show 方法
  33.     obj.show(); // 输出 DerivedC 的 show 方法
  34.     // 访问基类的 show 方法
  35.     obj.DerivedA::show(); // 明确调用 DerivedA 的 show 方法
  36.     obj.DerivedB::show(); // 明确调用 DerivedB 的 show 方法
  37.     return 0;
  38. }
复制代码
在菱形继续中,DerivedC 通过 DerivedA 和 DerivedB 都继续了 Base 类的成员。假如不明白指定,编译器无法确定应该调用 DerivedA 还是 DerivedB 中的 Base 类成员,这就导致了二义性。
假如加virtual就可以访问基类的 show 方法 obj.Base::show(); // 通过虚继续,只有一个 Base 的实例 。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表