函数的重载

函数的重载(function overloading):

C++允许用同一个函数名定义多个函数,而这些函数的参数个数和参数类型可以不相同。

一个函数名重新赋予它新的含义,使得一个函数名可以多用。

重载函数的参数个数、参数类型或参数顺序三者必须至少有一种不同,函数返回值类型可以相同可以不同。

//求三个数中的最大数
#include<iostream> using namespace std; int main(){ int max(int a,int b,int c); //整数 long max(long a,long b,long c); //双精度 double max(double a,double b,double c); //长整数 int i1,i2,i3,i; cin>>i1>>i2>>i3; i=max(i1,i2,i3); cout<<"i_max="<<i<<endl; int d1,d2,d3,d; cin>>d1>>d2>>d3; d=max(d1,d2,d3); cout<<"d_max="<<d<<endl; long g1,g2,g3,g; cin>>g1>>g2>>g3; g=max(g1,g2,g3); cout<<"g_max="<<g<<endl; } int max(int a,int b,int c){ if(b>a) a=b; if(c>a) a=c; return a; } long max(long a,long b,long c){ if(b>a) a=b; if(c>a) a=c; return a; } double max(double a,double b,double c){ if(b>a) a=b; if(c>a) a=c; return a; }

 

posted @ 2019-09-04 21:46  流转~星云  阅读(289)  评论(0编辑  收藏  举报