C++函数重载

本文介绍了C++中的函数重载概念,强调函数返回值类型不同不能作为重载依据。通过摄氏与华氏温度转换的例子,展示了如何根据参数类型重载函数,以提高代码的灵活性和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++的函数重载,就是同样的函数名,使其拥有接收不同的参数类型或者参数个数。都是重载函数!

需要注意的是:函数的返回值不同,是不能作为重载函数的!!!!

下面就是实现一个简单的参数不同的函数重载

 

#include <iostream>

int overloading(int a);
int overloading(int a, int b);
int overloading(int a, int b, int c);

int main()
{
	int a, b, c;
	std::cout << "请输入一个整数【X】,计算出X的平方" << std::endl;
	std::cin >> a;
	std::cout << overloading(a) << std::endl;
	
	std::cout << "请输入两个整数【X X】,计算出X*X的积" << std::endl;
	std::cin >> a >> b ;
	std::cout << overloading(a,b) << std::endl;

	std::cout << "请输入三个整数【X X X】,计算出X+X+X的和" << std::endl;
	std::cin >> a >> b >> c;
	std::cout << overloading(a,b,c) << std::endl;

	return 0;
}


int overloading(int a)
{
	int x;
	x = a * a;
	return x;
}
int overloading(int a, int b)
{
	int x;
	x = a * b;
	return x;
}
int overloading(int a, int b, int c)
{
	int x;
	x = a + b + c;
	return x;
}

 

 

我们实现一个摄氏温度和华氏温度的转换小程序,再一步步为了方变,把函数重载用在程序中

 

(1)接收用户输入的double类型的数据。xx.xx C 和xx.xx F  

判断用户输入的数据里面带C和c,就是摄氏温度,我们就转成华氏温度一并输出来。反之也一样

转换规律是:摄氏温度*9/5+32 = 华氏温度

 



#include <iostream>

int main()
{
	const unsigned short  ADD_PANT = 32;
	const double RADIO = 9.0 / 5.0;

	double tempIn, tempOut;
	char typeIn, typeOut;
	int  n = 0;

	do
	{
		std::cout << "请按此格式【xx.x C】或者【xx.x F】输入需要转换的温度!" << std::endl;
		std::cin >> tempIn >> typeIn;
		std::cin.ignore(100, '\n');

		switch (typeIn)
		{
		case 'F':
		case 'f':
			tempOut = (tempIn - ADD_PANT)*RADIO;
			typeIn = 'F';
			typeOut = 'C';
			n = 0;
			break;
		case 'C':
		case 'c':
			tempOut = tempIn * RADIO + ADD_PANT;
			typeIn = 'C';
			typeOut = 'F';
			n = 0;
			break;
		default:
			typeOut = 'E';
			n = 1;
			break;
		}
	} while (n == 1);

	std::cin.ignore(100, '\n');

		if (typeOut != 'E')
		{
			std::cout << tempIn << " " << typeIn << " = " << tempOut << " " << typeOut << std::endl;
		}
		else
		{
			std::cout << "输入不符合格式" << std::endl;
		}

	std::cout << "请输入任意字符结束程序:" << std::endl;
	std::cin.get();
	return 0;
}

 

我们把转换的逻辑封装下,得到以下的改变:

 

#include <iostream>
void temperaure(double tempIn, char typeIn);
int main()
{


	double tempIn;
	char typeIn;

	std::cout << "请按此格式【xx.x C】或者【xx.x F】输入需要转换的温度!" << std::endl;
	std::cin >> tempIn >> typeIn;
	std::cin.ignore(100, '\n');

	temperaure(tempIn, typeIn);


	return 0;
} 
void temperaure(double tempIn, char typeIn)
{

	const unsigned short  ADD_PANT = 32;
	const double RADIO = 9.0 / 5.0;

	double tempOut;
	char typeOut;



	switch (typeIn)
	{
	case 'F':
	case 'f':
		tempOut = (tempIn - ADD_PANT)*RADIO;
		typeIn = 'F';
		typeOut = 'C';
		break;
	case 'C':
	case 'c':
		tempOut = tempIn * RADIO + ADD_PANT;
		typeIn = 'C';
		typeOut = 'F';
		break;
	default:
		typeOut = 'E';
		break;
	}
	if (typeOut != 'E')
	{
		std::cout << tempIn << " " << typeIn << " = " << tempOut << " " << typeOut << std::endl;
	}
	else
	{
		std::cout << "输入不符合格式" << std::endl;
	}

	std::cout << "请输入任意字符结束程序:" << std::endl;
	std::cin.get();

}

 

 

 

既然转换的办法封装成函数了,我们就可以根据用户输入的数据来调用不同函数方法来实现转换

下面就是重载一个函数,可以接收用户输入的double类型的和整型的两种输入,都可以实现转换

 


#include <iostream>
void temperaure(double tempIn, char typeIn);
void temperaure(int tempIn, char typeIn);
int main()
{


	double tempIn;
	int tempInInt;
	char typeIn;

	std::cout << "请按此格式【xx.x C】或者【xx.x F】输入需要转换的温度!" << std::endl;
	std::cin >> tempIn >> typeIn;
	std::cin.ignore(100, '\n');

	temperaure(tempIn, typeIn);


	std::cout << "请按此格式【xx C】或者【xx F】输入需要转换的温度!" << std::endl;
	std::cin >> tempInInt >> typeIn;
	std::cin.ignore(100, '\n');

	temperaure(tempInInt, typeIn);

	return 0;
}
void temperaure(double tempIn, char typeIn)
{

	const unsigned short  ADD_PANT = 32;
	const double RADIO = 9.0 / 5.0;

	double tempOut;
	char typeOut;



	switch (typeIn)
	{
	case 'F':
	case 'f':
		tempOut = (tempIn - ADD_PANT)*RADIO;
		typeIn = 'F';
		typeOut = 'C';
		break;
	case 'C':
	case 'c':
		tempOut = tempIn * RADIO + ADD_PANT;
		typeIn = 'C';
		typeOut = 'F';
		break;
	default:
		typeOut = 'E';
		break;
	}
	if (typeOut != 'E')
	{
		std::cout << tempIn << " " << typeIn << " = " << tempOut << " " << typeOut << std::endl;
	}
	else
	{
		std::cout << "输入不符合格式" << std::endl;
	}

	std::cout << "请输入任意字符结束程序:" << std::endl;
	std::cin.get();

}



void temperaure(int tempIn, char typeIn)
{

	const short int ADD_PANT = 32;
	const double RADIO = 9.0 / 5.0;

	int tempOut;
	char typeOut;



	switch (typeIn)
	{
	case 'F':
	case 'f':
		tempOut = (tempIn - ADD_PANT)*RADIO;
		typeIn = 'F';
		typeOut = 'C';
		break;
	case 'C':
	case 'c':
		tempOut = tempIn * RADIO + ADD_PANT;
		typeIn = 'C';
		typeOut = 'F';
		break;
	default:
		typeOut = 'E';
		break;
	}
	if (typeOut != 'E')
	{
		std::cout << tempIn << " " << typeIn << " = " << tempOut << " " << typeOut << std::endl;
	}
	else
	{
		std::cout << "输入不符合格式" << std::endl;
	}

	std::cout << "请输入任意字符结束程序:" << std::endl;
	std::cin.get();

}

以上是我对函数重载的点点理解!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值