#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
class Base
{
public:
static int m_A; // 静态成员变量
static void func() // 静态成员函数
{
cout << “Base 下的 静态成员函数” << endl;
}
};
int Base::m_A = 200; // 初始化静态成员变量
class Son : public Base
{
public:
static int m_A; // 与父类同名的静态成员变量
static void func() // 与父类同名的静态成员函数
{
cout << “Son 下的 静态成员函数” << endl;
}
};
int Son::m_A = 100; // 初始化子类中同名的静态成员变量
void test01()
{
Son p;
// 通过对象来访问
cout << p.m_A << endl; // 未说明就是子类的
cout << p.Base::m_A << endl; // 使用作用域解析运算符访问父类的静态成员变量
// 通过类名进行访问
cout << Son::m_A << endl; // 直接访问子类的同名静态成员变量
cout << Son::Base::m_A << endl; // 使用两次作用域解析运算符访问父类的静态成员变量
}
void test02()
{
Son s;
s.func(); // 调用子类的静态成员函数
s.Base::func(); // 使用作用域解析运算符调用父类的静态成员函数
Son::func(); // 直接调用子类的静态成员函数
Son::Base::func(); // 使用两次作用域解析运算符调用父类的静态成员函数
}
int main()
{
// 在这里你可以调用 test01() 或 test02() 来测试上述代码的输出
}