202002XX-函数重载
-
#include "stdafx.h"
-
#include "iostream"
-
#include "cstring"
-
#include "string"
-
using namespace std;
-
struct job
-
{
-
char name[40];
-
double salary;
-
int floor;
-
};
-
int jzj(int a,int b);
-
int jzj(int a,int b,int c);
-
int main()
-
{
-
int a=1,b=1,d=2;
-
cout<<jzj(a,b)<<endl;
-
cout<<jzj(a,b,d)<<endl;
-
}
-
int jzj(int d,int b)
-
{
-
return d+b;
-
}
-
int jzj(int a,int b,int c)
-
{
-
return a+b+c;
-
-
}
运行结果:
代码2:
代码2:
#include <iostream>
template <typename T>
void ShowArray(T arr[],int n);
template <typename T>
void ShowArray(T * arr[],int n);
struct debts{
char name[50];
double amount;
};
using namespace std;
int main(int argc, char** argv){
int things[6]={13,31,103,301,310,130};
struct debts mr_E[3]=
{
{"ima wolfe",2400.0},
{"ura foxe",1300.0},
{"iby stout",1800.0}
};
double * pd[3];
for (int i=0;i<=2;i++)
pd[i]=&mr_E[i].amount;//set pointers to thd amount members of the structures in mr_E
cout<<"Listing Mr.E's counts of things:\n";
//things ins an array of int
ShowArray(things,6);//used template A
cout<<"Listing Mr.E's debts:\n";
ShowArray(pd,3);
return 0;
}
template <typename T>
void ShowArray(T arr[],int n){
for(int i=0;i<n;i++)
cout<<arr[i]<<' '<<endl;
}
template <typename T>
void ShowArray(T * arr[],int n){
for(int i=0;i<n;i++)
cout<<*arr[i]<<' '<<endl;
}
代码3:
代码3:
#include <iostream>
using namespace std;
template <class T>
T lesser(T a,T b)//1#
{
return a<b?a:b;
}
int lesser(int a,int b)//2#
{
a=a<0?-a:a;
b=b<0?-b:b;
return a<b?a:b;
}
int main()
{
int m=20;
int n=-30;
double x=15.5;
double y=25.9;
cout<<lesser(m,n)<<endl;//use 2#,20
cout<<lesser(x,y)<<endl;//use 1# 15.5
cout<<lesser<>(m,n)<<endl; //use 1# -30
cout<<lesser<int>(x,y)<<endl;//use 1# double-int