C++关于重载、重写和覆盖的一些实验(2)覆盖和重写

当雾霾和温暖一起来到北京的时候,我不知道该爱它还是该恨它。我不知道的事情还有太多,我要试着去知道。今天就试着知道一下函数覆盖吧!
今天先做个试验,代码分三个文件 override.h(声明) override.cpp(实现) main.cpp(测试)
首先在override中实现两个类基类和子类中只有一个成员函数,且该成员函数被virtual修饰
override.h内容:


#ifndef OVERRIDE_H
#define OVERRIDE_H
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
enum EKeyWord{eHello=0,eHi,eNice};
class COverrideBase
{
public:
	COverrideBase(){};
	virtual ~COverrideBase(){};
	virtual void whoAreYou(); //无参数无返回值的虚函数 1

	virtual void whoAreYou(EKeyWord);//含参数无返回值的虚函数	2
	void whoAreYou(string s);	//带string参数无返回值的函数	3

	virtual void myNameIs();//无参数无返回值的虚函数	4	

	int tellTruth();//无参数有返回值 5

	int whoAmI();//无参数有返回值 6
	void whoAmI(string);//带string参数无返回值 7

};
class COverride:public COverrideBase
{
public:
	COverride(){};
	virtual ~COverride(){};
	void whoAreYou();	//无参数无返回值 对应基类函数1

	void whoAreYou(EKeyWord);//含参数无返回值 对应基类函数2

	void whoAreYou(string s);	//带string参数无返回值的函数	3

	int myNameIs();//返回值int无参数 对应基类的函数4

	void tellTruth();//无参数无返回值 对应基类函数5

	int whoAmI();//无参数有返回值 6
	void whoAmI(string);//带string参数无返回值 7

};

#endif

override.cpp内容:

#include "override.h"
void COverrideBase::whoAreYou()
{
	cout<<"我是基类函数virtual void whoAreYou()"<<endl;
}

void COverrideBase::whoAreYou(string s)
{
	cout<<"我是基类函数 void whoAreYou(string s)"<<endl;
}

void COverrideBase::whoAreYou(EKeyWord indexK)
{
	char *sWord[]={"Hello!","Hi!","Nice to meet you!"};
	cout<<sWord[indexK]<<"我是基类函数virtual void whoAreYou(EKeyWord)"<<endl;
}
void COverrideBase::myNameIs()
{
	cout<<"我是基类函数void myNameIs()"<<endl;
}
int COverrideBase::tellTruth()
{
	cout<<"我是基类函数int tellTruth()"<<endl;
	return 0;
}
int COverrideBase::whoAmI()
{
	cout<<"我是基类函数int whoAmI()"<<endl;
	return 0;
}
void COverrideBase::whoAmI(string s)
{
	cout<<"我是基类函数void whoAmI(string)"<<endl;
}

void COverride::whoAreYou(string s)
{
	cout<<"我是子类函数void whoAreYou(string s)"<<endl;
}
void COverride::whoAreYou()
{
	cout<<"我是子类函数virtual void whoAreYou()"<<endl;
}
void COverride::whoAreYou(EKeyWord indexK)
{
	char *sWord[]={"Hello!","Hi!","Nice to meet you!"};
	cout<<sWord[indexK]<<"我是子类函数virtual void whoAreYou(EKeyWord)"<<endl;
}
int COverride::myNameIs()
{
	cout<<"我是子类函数int myNameIs()"<<endl;
	return 0;
}
void COverride::tellTruth()
{
	cout<<"我是子类函数void tellTruth()"<<endl;
}
int COverride::whoAmI()
{
	cout<<"我是子类函数int whoAmI()"<<endl;
	return 0;
}
void COverride::whoAmI(string s)
{
	cout<<"我是子类函数void whoAmI(string)"<<endl;
}

main.cpp

#include "override.h"
int main(int argc,char** argv)
{
	COverride* overInstance = new COverride;
	COverrideBase* overbaseInstance = dynamic_cast<COverrideBase*>(overInstance);
	//比较函数1 是否覆盖
	overInstance->whoAreYou();
	overbaseInstance->whoAreYou();
	//比较函数2 是否覆盖
	overInstance->whoAreYou(EKeyWord::eHello);
	overbaseInstance->whoAreYou(EKeyWord::eNice);
	//比较函数3 是否覆盖
	overInstance->whoAreYou("yyy");
	overbaseInstance->whoAreYou("yyy");
	//比较函数4 是否覆盖
	overInstance->myNameIs();
	overbaseInstance->myNameIs();
	//比较函数5 是否覆盖
	overInstance->tellTruth();
	overbaseInstance->tellTruth();
	//比较函数6 是否覆盖
	overInstance->whoAmI();
	overbaseInstance->whoAmI();
	//比较函数7 是否覆盖
	overInstance->whoAmI("yyy");
	overbaseInstance->whoAmI("yyy");

	char a;
	cin>>a;
	return 0;
}

编译这三个文件,程序报错:
错误 2 error C2555: “COverride::myNameIs”: 重写虚函数返回类型有差异,且不是来自“COverrideBase::myNameIs”的协变
通过此编译错误得到结论1:当成员函数的函数名和函数参数完全相同,但返回值类型不同时,无法实现覆盖或重写。
修改基类成员函数声明,去掉virtual修饰使之成为重写写函数,重新编译。
此时可以编译通过,得到如下结果:

我是子类函数virtual void whoAreYou()
我是子类函数virtual void whoAreYou()
Hello!我是子类函数virtual void whoAreYou(EKeyWord)
Nice to meet you!我是子类函数virtual void whoAreYou(EKeyWord)
我是子类函数void whoAreYou(string s)
我是基类函数 void whoAreYou(string s)
我是子类函数int myNameIs()
我是基类函数void myNameIs()
我是子类函数void tellTruth()
我是基类函数int tellTruth()
我是子类函数int whoAmI()
我是基类函数int whoAmI()
我是子类函数void whoAmI(string)
我是基类函数void whoAmI(string)

比较函数1和函数2可以发现:函数参数不同的成员函数覆盖时互不影响,独自覆盖。
比较函数1和函数3发现:成员函数未被virtual修饰时,将会进行覆盖而不是重载。
比较函数4(或者函数5)和函数6 函数7发现:只要函数名相同基类的函数就会被子类重写;
将子类的函数7注释掉,重新编译,发现程序找不到子类对象的void whoAmI(string)但是基类对象却正常。
好了,基本对于重写,覆盖以及昨天的重载有了一个比较深刻的体会了;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值