栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 继承 学习总结3 继承中父类和子类同名非静态成员或者同名静态成员的处理方式

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 继承 学习总结3 继承中父类和子类同名非静态成员或者同名静态成员的处理方式

1.继承中父类和子类有同名非静态成员的处理方式

eg:

#include 
using namespace std;
class Base {
public:
	Base()
	{
		m_A = 100;
	}

	void func()
	{
		cout << "Base - func()调用" << endl;
	}

	void func(int a)
	{
		cout << "Base - func(int a)调用" << endl;
	}

public:
	int m_A;
};


class Son : public Base {
public:
	Son()
	{
		m_A = 200;
	}

	//当子类与父类拥有同名的成员函数,子类会隐藏父类中所有版本的同名成员函数
	//如果想访问父类中被隐藏的同名成员函数,需要加父类的作用域
	void func()
	{
		cout << "Son - func()调用" << endl;
	}
public:
	int m_A;
};

void test01()
{
	Son s;

	cout << "Son下的m_A = " << s.m_A << endl;
	cout << "Base下的m_A = " << s.Base::m_A << endl;

	s.func();
	s.Base::func();
	s.Base::func(10);

}
int main() {

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

例子中:

子类和父类都有m_A的成员变量,

访问子类的m_A直接访问(s.m_A),访问父类的m_A需要加作用域(s.Base::m_A).

子类和父类都有func()成员,

访问子类的m_A直接访问(s.func()),访问父类的m_A需要加作用域(s.Base::func()).

特殊的是:

即使父类中存在同名函数func(int a),表面上看起来可能会发生函数重载,我们想通过s.func(1)这样的方法直接访问父类中的func(int a)函数。

但实际上当子类和父类有同名函数存在的时候,系统会自动屏蔽父类中的所有版本的同名成员函数,即使是函数重载版本的同名成员函数,若想访问,依旧需要加父类作用域,例如s.Base::func(1)。

2.继承中父类和子类有同名静态成员的处理方式
#include 
using namespace std;
class Base {
public:
	static void func()
	{
		cout << "Base - static void func()" << endl;
	}
	static void func(int a)
	{
		cout << "Base - static void func(int a)" << endl;
	}

	static int m_A;
};

int Base::m_A = 100;

class Son : public Base {
public:
	static void func()
	{
		cout << "Son - static void func()" << endl;
	}
	static int m_A;
};

int Son::m_A = 200;

//同名成员属性
void test01()
{
	//通过对象访问
	cout << "通过对象访问: " << endl;
	Son s;
	cout << "Son  下 m_A = " << s.m_A << endl;
	cout << "Base 下 m_A = " << s.Base::m_A << endl;

	//通过类名访问
	cout << "通过类名访问: " << endl;
	cout << "Son  下 m_A = " << Son::m_A << endl;
	cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}

//同名成员函数
void test02()
{
	//通过对象访问
	cout << "通过对象访问: " << endl;
	Son s;
	s.func();
	s.Base::func();

	cout << "通过类名访问: " << endl;
	Son::func();
	Son::Base::func();
	//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
	Son::Base::func(100);
}
int main() {

	//test01();
	test02();

	system("pause");

	return 0;
}

同名静态成员处理方式和非静态成员处理方式一样,可以参照上面同名非静态成员的处理方式。

但由于静态成员自身的特性

(静态成员变量:1.数据只有一份,所有对象都共享同一份数据

                             2. 编译阶段就分配内存

                             3.类内声明,类外初始化

    静态成员函数:1.只能访问静态成员变量,不能访问非静态成员变量

                              2.所有对象都共享同一份函数实例)

存在两种访问方式(通过对象访问和通过类名访问),这里着重说一下这两种方式。

eg:

#include 
using namespace std;
class Base {
public:
	static void func()
	{
		cout << "Base - static void func()" << endl;
	}
	static void func(int a)
	{
		cout << "Base - static void func(int a)" << endl;
	}

	static int m_A;
};

int Base::m_A = 100;

class Son : public Base {
public:
	static void func()
	{
		cout << "Son - static void func()" << endl;
	}
	static int m_A;
};

int Son::m_A = 200;

//同名成员属性
void test01()
{
	//通过对象访问
	cout << "通过对象访问: " << endl;
	Son s;
	cout << "Son  下 m_A = " << s.m_A << endl;
	cout << "Base 下 m_A = " << s.Base::m_A << endl;

	//通过类名访问
	cout << "通过类名访问: " << endl;
	cout << "Son  下 m_A = " << Son::m_A << endl;
	cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}

//同名成员函数
void test02()
{
	//通过对象访问
	cout << "通过对象访问: " << endl;
	Son s;
	s.func();
	s.Base::func();

	cout << "通过类名访问: " << endl;
	Son::func();
	Son::Base::func();
	//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
	Son::Base::func(100);
}
int main() {

	//test01();
	test02();

	system("pause");

	return 0;
}

通过对象访问:

1.访问静态成员变量

s.m_A

s.Base::m_A

2.访问静态成员函数

s.func()

s.Base::func()
 

通过类名访问

1.访问静态成员变量

s::m_A

s::Base::m_A (访问s类作用域下的Base父类的m_A静态成员变量) (注意区分两个冒号的区别)

2.访问静态成员函数

s::func()

s::Base::func()(访问s类作用域下的Base父类的func()静态成员函数) (注意区分两个冒号的区别)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867898.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号