class person {
public:
void func() {
cout <<"_name "<< _name << endl;
cout << "_age " << _age << endl;
}
protected:
string _name="abc";
int _age=0;
};
class student :public person {//public就是继承方式
protected:
int _s_id;
};
class teacher :public person {
protected:
int _t_id;
};
int main() {
student s1;
s1.func();
return 0;
}
类成员/继承方式 | public继承 | protected继承 | private继承 |
基类的public成员 | 派生类的public成员 | 派生类的protected成员 | 派生类的private成员 |
基类的protected成员 | 派生类的protected成员 | 派生类的protected成员 | 派生类的private成员 |
基类的private成员 | 在派生类中不可见 | 在派生类中不可见 | 在派生类中不可见 |
class person {
public:
person(const char* s="Tony", int n=10)
:_name(s)
,_age(n)
{}
protected:
string _name;
int _age;
};
class student :public person {
public:
student(const char* s = "Tony", int age = 10, int id = 1)
:_s_id(id)
, person(s, age)
{}
protected:
int _s_id;
};
class A {
public:
//protected:
int _a;
};
class B :public A{
public:
//protected:
int _b;
};
class C:public A {
public:
//protected:
int _c;
};
class D :public B, public C {
public:
//protected:
int _d;
};
int main() {
D dd;
dd._a = 10;//不明确
return 0;
}