202002XX-函数重载

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "cstring"

  4. #include "string"

  5. using namespace std;

  6. struct job

  7. {

  8.   char name[40];

  9.   double salary;

  10.   int floor;

  11. };

  12. int jzj(int a,int b);

  13. int jzj(int a,int b,int c);

  14. int main()

  15. {  

  16. int a=1,b=1,d=2;

  17. cout<<jzj(a,b)<<endl;

  18. cout<<jzj(a,b,d)<<endl;

  19. }

  20. int jzj(int d,int b)

  21. {

  22.   return d+b;

  23. }

  24. int jzj(int a,int b,int c)

  25. {

  26.   return a+b+c;

  27.  

  28. }

运行结果:



代码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:
#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
posted @ 2020-05-21 08:26  财盛  阅读(129)  评论(0编辑  收藏  举报