1. 什么是继承
    1. 继承就是对数据的复制
  2. 为什么要用
  3. 减少重复代码的编写
  4. Person 称为父类或者基类
  5. teacher 称为子类或者派生类 ```cpp

    include

    include

    include

struct Person {
int age; int sex; };

struct Teacher:Person//继承 { int level; int classid; }; int main(int argc, char* argv[]) {

  1. Teacher t;
  2. t.age = 1;
  3. t.sex = 2;
  4. t.level = 3;
  5. t.classid = 4;
  6. int x = sizeof(t);// t 的大小为16 ,t继承了person的数据
  7. return 0;

}

![image.png](https://cdn.nlark.com/yuque/0/2021/png/2616496/1616832466305-0c742a89-21aa-4be3-9926-34dcfe694122.png#align=left&display=inline&height=92&margin=%5Bobject%20Object%5D&name=image.png&originHeight=92&originWidth=548&size=5093&status=done&style=none&width=548)
```cpp
    Teacher t;
    t.age = 1;
005F48C2  mov         dword ptr [t],1  
    t.sex = 2;
005F48C9  mov         dword ptr [ebp-14h],2  
    t.level = 3;
005F48D0  mov         dword ptr [ebp-10h],3  
    t.classid = 4;
005F48D7  mov         dword ptr [ebp-0Ch],4  
    int x = sizeof(t);
005F48DE  mov         dword ptr [x],10h  
    return 0;
005F48E5  xor         eax,eax  
}

重载

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Person
{    
    int age;
    int sex;
};

struct Teacher:Person//继承
{
    int age;
    int classid;
};
int main(int argc, char* argv[]) {

    Teacher t;
    t.age = 1;
    t.Person::age = 2; //同名时
    t.sex = 2;
    t.classid = 4;
    int x = sizeof(t);
    return 0;
}
Teacher t;
    t.age = 1;
00D848C2  mov         dword ptr [ebp-10h],1  
    t.Person::age = 2;
00D848C9  mov         dword ptr [t],2  
    t.sex = 2;
00D848D0  mov         dword ptr [ebp-14h],2  
    t.classid = 4;
00D848D7  mov         dword ptr [ebp-0Ch],4  
    int x = sizeof(t);
00D848DE  mov         dword ptr [x],10h  
    return 0;
00D848E5  xor         eax,eax  
}

image.png
多继承 -多层继承
多重继承增加程序复杂度,不建议使用

image.png