- 公共继承
- 父类中私有权限,子类不能访问
- 父类中保护权限,子类中变为保护权限
- 父类中公共权限,子类中变为公共权限
- 保护继承
- 父类中公共权限,子类中变为保护权限
- 父类中保护权限,子类中变为保护权限
- 父类中私有权限,子类不能访问
- 私有继承
- 父类中公共权限,子类中变为私有权限
- 父类中保护权限,子类中变为私有权限
- 父类中私有权限,子类不能访问
注意:
父类中私有属性也会继承,但是在子类中无法访问
父类中,构造函数、析构函数、重载不会被继承
#include<iostream>
using namespace std;
class structure {
private:
char pageName[30];
public:
void header() {
cout << "页面头部公共部分" << endl;
}
void aside() {
cout<<"页面侧边栏公共部分" << endl;
}
void footer() {
cout << "页面尾部公共部分" << endl;
}
protected:
char serverName[30];
};
//公共继承
class news:public structure {
private:
public:
void desc() {
strcpy_s(serverName, 30, "apache");
//strcpy_s(pageName, 30, "index");
cout << "新闻列表" << endl;
}
};
//保护继承
class sports:protected structure {
private:
public:
void desc() {
strcpy_s(serverName, 30, "apache");
cout << "体育列表" << endl;
}
};
//私有继承
class games :private structure {
private:
public:
void desc() {
strcpy_s(serverName, 30, "apache");
cout << "游戏列表" << endl;
cout << "server " << serverName << endl;
}
};
class test {
public:
void t() {
//strcpy_s(serverName,30, "apache");
}
};
void test() {
news n1;
n1.header();
n1.aside();
n1.desc();
n1.footer();
cout << "next" << endl;
sports s1;
//s1.header();
//s1.aside();
s1.desc();
//s1.footer();
cout << "next" << endl;
games g1;
g1.desc();
}
int main() {
test();
system("pause");
return EXIT_SUCCESS;
}