//c++里的单继承
#include <iostream>
using namespace std;
class father
{
private:
int a;
int b;
public:
int c;
int d;
protected:
int e;
int*p;
public:
void setA(int newA)
{
a=newA;
};
void showA(void)
{
cout<<a<<"father show A"<<endl;
}
};
class mother
{
private:
int a;
public:
void setA(int newA)
{
a=newA;
}
void showA(void)
{
cout<<a<<"mother show A"<<endl;
}
};
//默认是私有继承
//多继承
class Son:public father,public mother
{
public:
void func(void)
{
c=9;
e=10;
};
private:
int A;
void fuc11(void)
{
father::c=10;
};
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
Son son;
son.father::setA(9999);
son.father::showA();
cout<<endl;
son.mother::showA();
//继承于父类的方法,可以访问父类的私有变量
//继承是怎么发生的呢?
//所谓的继承,其实是继承了父类的所有变量,之时编译器做了权限设置,
//父类访问父类的变量是是给前四个字节,
//继承的子类也能访问前四个字节
//子类(父类的空间+自己独有的空间,)父类的空间在前,子类在后
cout<<sizeof(father)<<endl;
cout<<sizeof(son)<<endl;
//子类的空间要比父类的空间大
return 0;
}
C++多继承
最新推荐文章于 2025-12-02 18:24:55 发布
1074

被折叠的 条评论
为什么被折叠?



