记录C++中,子类同名属性并不能完全覆盖父类属性的问题

问题代码:

首先看一段代码:很简单,就是BBB继承自AAA,然后BBB重写定义了同名属性,然后调用父类AAA的打印函数:

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	void print1() {
		cout << "YourVar: " << YourVar << endl;
	}
	
	void YourMethodOrFunction() {}
	
private:
	int YourVar;
};

int main() {
	BBB b;
	b.print();
	//b.print1();
	
	cout << "********" << endl;
	
	return 0;
}

打印结果:

会发现,明明我构造函数里面将YourVar的值改为了20,但是还是打印的10.

这个问题,属于C++的基础问题,我记得,子类继承父类,如果子类中,定义了同名属性的话,那么父类的属性会被隐藏,访问子类的那个属性,就是子类的,不会是父类的。

但是如果父类里面,有一个打印的函数print,会发现,这个print函数,生效的范围,只有父类,不包括子类。只有将父类的print函数重写,才能生效在子类中:

解决思路1:重写print

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
	void YourMethodOrFunction() {}
	
private:
	int YourVar;
};

int main() {
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

打印结果:

虽然解决了问题,但是明明我父类中,写了print函数,干嘛还重新写一次?还有意义?

解决思路2:去掉子类的同名属性

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	
//private:
//	int YourVar;
};

int main() {
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

但是有时候,我们就是要这么重写,那么该怎么办呢?

解决思路3:将父类地址传递给子类

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		YourVar = new int;
		*YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {

		// 将父类地址传递给子类
		YourVar = AAA::YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

解决思路4:将子类地址传递给父类:

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		// YourVar = new int;
		// *YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = new int;
		// 将子类地址传递给父类
		AAA::YourVar = YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

总结

  1. 面对这样的情况,要么就不要写同名属性(但有时候不能保证自己忘记了)

  2. 当以后继承的类层次很多的时候,难免会定义同名属性,但是父类的函数,是不会访问子类的同名属性的,这个是这个问题的关键。虽然在子类中,父类的属性确实是隐藏了,但隐藏不代表消失,也不代表父类的函数中,也将这个属性给隐藏或者替换了。

  3. 要么就使用指针,将父类和子类的地址变成一样的地址。

  4. 虽然最后一种比较麻烦,但是会惊奇的发现,如果是指针的话,可以实现父类中实现所有操作,子类只管创建对象和使用,子类如果想改实现,根据需要同名替代即可。甚至属性也可以写成函数指针那些,是一种非常灵活的使用方式,是一种上层设计模式的简单体现。

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		// YourVar = new int;
		// *YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = new int;
		// 将子类地址传递给父类
		AAA::YourVar = YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
	void print() {
		cout << "*********************" << endl;
		cout << "YourVar: " << *YourVar << endl;
		cout << "*********************" << endl;
	}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	return 0;
}
相关推荐
珊瑚里的鱼26 分钟前
第九讲 | 模板进阶
开发语言·c++·笔记·visualstudio·学习方法·visual studio
未来之窗软件服务36 分钟前
人体肢体渲染-一步几个脚印从头设计数字生命——仙盟创梦IDE
开发语言·ide·人工智能·python·pygame·仙盟创梦ide
Echo``44 分钟前
40:相机与镜头选型
开发语言·人工智能·深度学习·计算机视觉·视觉检测
摄殓永恒1 小时前
猫咪几岁
数据结构·c++·算法
lisw051 小时前
R语言的专业网站top5推荐
开发语言·r语言
清同趣科研1 小时前
扩增子分析|R分析之微生物生态网络稳定性评估之节点和连接的恒常性、节点持久性以及组成稳定性指数计算
开发语言·r语言
纨妙1 小时前
python打卡打印26
开发语言·python
.小墨迹2 小时前
Apollo学习——键盘控制速度
linux·开发语言·c++·python·学习·计算机外设
似水এ᭄往昔2 小时前
【数据结构】——队列
c语言·数据结构·c++·链表
烛九_阴2 小时前
【C++】解析C++面向对象三要素:封装、继承与多态实现机制
c++