#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Base
{
public:
static int A;
int B{};
static void func(){cout << "Base的静态成员函数调用" << endl;}
static void func(const int& m_A)
{
A = m_A;
cout << "调用Base静态成员函数后A = " << A << endl;
}
protected:
private:
};
int Base::A = 10;
class Son : public Base
{
public:
static int A;
int B{};
static void func(){cout << "Son的静态成员函数的调用" << endl;}
static void func(const int& m_A)
{
A = m_A;
cout << "调用Son静态成员函数后A = " << A << endl;
}
protected:
private:
};
int Son::A = 20;
void test01()
{
//cout << Base::B << endl; 非静态成员不能通过类名访问
Son s1;
cout << s1.A << endl;
cout << s1.Base::A << endl; //必须添加作用域才能访问父类中的同名成员变量
}
void test02()
{
//通过对象访问:
Son s2;
s2.func();
s2.Base::func();
s2.Base::func(2000);
//通过类进行访问:
Son::func(50);
Son::func();
Son::Base::func();
Son::Base::func(500);
}
int main()
{
test01();
test02();
return 0;
}
|