继承的语法:
class A : 继承方式 B
继承的方式:
公共继承 public(除了私有内容无法访问 其他内容访问权限不变 )
保护继承 protected(除了私有内容无法访问 其他内容都变为保护权限)
私有继承 private(除了私有内容无法访问 其他内容都变为私有权限)
#include<iostream>
#include<cstdio>
using namespace std;
class A{
public:
int _a = 10;
protected:
int _b = 11;
private:
int _c = 12; // 父类 非常隐私的东西 无论子类如何继承也继承不到
};
class B: public A{ // 除了私有内容无法访问 其他内容访问权限不变
public:
void show(){
cout<<this->_a<<" "<<this->_b<<endl;
}
};
class C: protected A{ // 除了私有内容无法访问 其他内容都变为保护权限
public:
void show(){
cout<<this->_a<<" "<<this->_b<<endl;
}
};
class D: private A{ // 除了私有内容无法访问 其他内容都变为私有权限
public:
void show(){
cout<<this->_a<<" "<<this->_b<<endl;
}
};
int main(){
B b = B();
cout<<b._a<<endl;
b.show();
C c = C();
c.show();
D d = D();
d.show();
return 0;
}