#include <stdio.h>
#include "stdafx.h"
#include <iostream>
using namespace std;
// 函数指针实现
int jug(int x, int y)
{
if (x >= 0)
{
return x;
}
else if (y == 0)
{
return x;
}
else
return x / y;
}
int add(int x, int y)
{
return (x + y);
}
int mul(int x, int y)
{
return (x*y );
}
//函数指针
int test(int (*p)(int, int), int a, int b)
{
int Int1;
Int1 = (*p)(a, b);
return Int1;
// printf("a = %d, b =%d, result = %d", a, b, Int1);
}
//静态模板类
template<class T>
class Operate
{
public:
static T Add(T a, T b)
{
return a+b;
}
static T Mul(T a, T b)
{
return a*b;
}
static T Judge(T a, T b)
{
if (a >= 0)
{
return a;
}
else if (b==0)
{
return a;
}
else
return a/b;
}
};
int main()
{
int a, b, c, d, e, x1, y1, z1, x2, y2, z2;
a=1, b=2, c=3, d= 4, e = 5;
x1=test(add, a, b);
y1=test(mul, c, d);
z1=test(jug, e,b);
cout << x1 << '\n' << y1 << '\n' << z1 << endl;
x2=Operate<int>::Add(a,b);
y2=Operate<int>::Mul(c,d);
z2=Operate<int>::Judge(e, b);
cout << x2 << '\n' << y2 << '\n' << z2 << endl;
system("PAUSE");
return 0;
}
函数指针与模板例子
最新推荐文章于 2022-09-08 17:44:20 发布