#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
//父类
class Base
{
public:
static void func()
{
cout << "Bfunc正在被调用" << endl;
}
public:
static int m_A;//静态成员类内声明
};
int Base::m_A = 100;//静态成员定义
class Son :public Base
{
public:
static void func()
{
cout << "Sfunc正在被调用" << endl;
}
static int m_A;
};
int Son::m_A = 200;
void test()
{
cout << "类名访问"<<endl;
cout << Son::m_A << endl;
cout << Son::Base::m_A << endl;
Son::func();
Son::Base::func();
}
void test2()
{
cout << "对象访问" << endl;
Son s;
cout << s.m_A << endl;
cout << s.Base::m_A << endl;
s.func();
s.Base::func();
}
int main()
{
test2();
test();
return 0;
}
C++类与对象 -继承-同名静态成员的处理
最新推荐文章于 2023-04-28 15:21:08 发布