一、什么是继承
继承就是数据的复制
struct Person
{
int age;
int sex;
};
struct Teacher:Person
{
int level;
int classId;
};
struct Student:Person
{
int code;
int score;
};
Teacher t;
t.age = 1;
t.sex = 2;
t.level = 3;
t.classId = 4;
printf("%d\n",sizeof(t)); //大小为16, 将父类复制一份给自己
二、多层继承
多层继承:
struct X
{
int a;
int b;
};
struct Y:X
{
int c;
int d;
};
struct Z:Y
{
int e;
int f;
};
Z z;
z.a = 1;
z.b = 2;
z.c = 3;
z.d = 4;
z.e = 5;
z.f = 6;
printf("%d\n",sizeof(z)); //大小为24,父类复制一份给自己
三、多重继承
多重继承:
struct X
{
int a;
int b;
};
struct Y
{
int c;
int d;
};
struct Z:X,Y
{
int e;
int f;
};
Z z;
z.a = 1;
z.b = 2;
z.c = 3;
z.d = 4;
z.e = 5;
z.f = 6;
printf("%d\n",sizeof(z)); //大小为24