#include<iostream>
using namespace std;
class Base1 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1 {
public:
void func() {
m_A;//可访问 public权限
m_B;//可访问 protected权限
//m_C;//不可访问
}
};
void myClass() {
Son1 s1;
s1.m_A;//public类内类外都可访问
}
class Base2
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2 :protected Base2 {
public:
void func() {
m_A; //可访问 protected权限
m_B; //可访问 protected权限
//m_C; //不可访问
}
protected:
void func2() {
m_B;
}
};
void myClass2() {
Son2 s;
//s.m_A;//protected可以在类内访问 类外就不行了
}
class Base3 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3 :private Base3 {
public:
void func() {
m_A;//可访问 private权限
m_B;//可访问 private权限
//m_C;//不可访问
}
};
class GrandSon3 :public Son3 {
public:
void func() {
//Son3是私有继承,所以继承Son3的属性在GrandSon3中都无法访问到
//继承Son3 Son3继承私有属性 这个及继承的东西只能在本类使用 无法继承到下面了
}
};
c++ 继承使用(简易版)
最新推荐文章于 2024-09-07 05:15:02 发布