我是靠谱客的博主 贪玩未来,这篇文章主要介绍C++:函数重载示例,现在分享给大家,希望可以做个参考。

// 函数重载示例20220503.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
//声明三个函数,函数名相同,形参类型不同
int myabs(int x);
float myabs(float x);
double myabs(double x);

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "函数重载示例" << endl;
	int a = -5;
	float b = -3.2f;
	double c = -4.75;
	cout << myabs(a) << endl;
	cout << myabs(b) << endl;
	cout << myabs(c) << endl;
	getchar();
	return 0;
}

int myabs(int x)
{
	cout << "int abs(int x) 被调用!" << endl;
	return  (x < 0) ? -x : x;
}
float myabs(float x)
{
	cout << "float abs(float x) 被调用!" << endl;
	return  (x < 0) ? -x : x;
}
double myabs(double x)
{
	cout << "double abs(double x) 被调用!" << endl;
	return  (x < 0) ? -x : x;
}

运行结果:

 

最后

以上就是贪玩未来最近收集整理的关于C++:函数重载示例的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部