#include<iostream>
using namespace std;
class student {
public:
int num;
private:
string name;
protected:
int sex;
};
class student1 :public student {
public:
void fun() {
num = 1;//继承过来是public
//name = "wc";//基类private的始终是无法访问
sex = 1;//继承过来还是protected
}
};
class student2 :protected student {
public:
void fun() {
num = 1; //继承过来是protected
//name = "wc";//基类private的始终是无法访问
sex = 1;//继承过来还是protected
}
};
class student3 :private student {
public:
void fun() {
num = 1; //继承过来是private
//name = "wc";//基类private的始终是无法访问
sex = 1;//继承过来还是private
}
};
int main() {
student t;
t.num;
//t.name;
//t.sex;类外无法访问
student1 t1;
t1.num;
//t1.name;
//t1.sex;类外无法访问
student2 t2;
/*t2.num;
t2.name;
t2.sex;*/// protected 类外无法访问
student3 t3;
//t3.num;
//t3.name;
//t3.sex;类外无法访问
}
注释挺详细的,另外补充一个表。
补充:继承不包括构造函数和析构函数.