继承中同名成员函数的访问总结
1)访问静态成员和访问非静态的方法一样:子类想要访问父类就加作用域,默认访问就访问子类
#include<iostream>
using namespace std;
class father
{
public:
static void fun_c();
static int variable;
};
void father::fun_c()
{
cout<<"this is father"<<endl;
}
int father::variable=10;
class son:public father
{
public:
static void fun_c();
static int variable;
};
void son::fun_c()
{
cout<<"this is son"<<endl;
}
int son::variable=20;
void test()
{
son son_obj;
son_obj.fun_c();//通过子类对象访问子类的同名成员函数
son::fun_c();//通过子类访问同名成员函数
cout<<son_obj.variable<<endl;//通过子类成员访问子类同名成员变量
cout<<"------------this is static rember---------------------------------------"<<endl;
son::father::fun_c();//通过子类访问父类的同名成员函数
son_obj.father::fun_c();//通过子类对象访问父类的同名成员函数
cout<<son_obj.father::variable<<endl;//通过子类对象访问父类的同名成员变量
cout<<son::father::variable<<endl;//通过子类访问父类的同名成员变量
}
int main()
{
test();
}