#include"inherit_03.h"
class MyBase
{
public:
MyBase()
{
a = 10;
b = 11;
}
static void funcA()
{
cout << "Base - static void funcA():" << endl;
}
static void funcB(int b)
{
cout << "Base - static void funcA(int b):" << b << endl;
}
static int a;
int b = 200;
};
// 赋值静态属性(静态成员类内声明,类外初始化)
int MyBase::a = 100;
class SonA:public MyBase
{
public:
SonA()
{
a = 20;
b = 21;
}
static void funcA()
{
cout << "SonA - static void funcA(int a):" << endl;
}
int a=300;
int b=400;
};
void inherit_03_test_01()
{
// 1.用实例调用
SonA A;
// 1.1调用对象本身的成员
cout <<"A.a: " << A.a << endl;//20
cout <<"A.b: " << A.b << endl;//21
A.funcA();
A.funcB(2);
// 1.2调用对象父类的成员
cout <<"A.MyBase::a: " << A.MyBase::a << endl;//10
cout <<"A.MyBase::b: " << A.MyBase::b << endl;//11
A.MyBase::funcA;
A.MyBase::funcB(3);
// 2.有类调用
// 用类调用属性
cout << "MyBase::a: " << MyBase::a << endl;
cout << "SonA::MyBase::a: " << SonA::MyBase::a << endl;
// 用类调用函数
SonA::MyBase::funcA();
SonA::MyBase::funcB(4);
}
52 C++ 继承同名成员处理方式
最新推荐文章于 2024-11-02 20:20:37 发布