#include <iostream>
#include <stdlib.h>
template <class T>
class Operation
{
private:
T x, y;
public:
Operation(T a, T b)
{
x = a;
y = b;
}
T add();
T sub();
};
template <class T>
T Operation<T>::add()
{
return x + y;
}
template <class T>
T Operation<T>::sub()
{
return x - y;
}
int main()
{
Operation<int> opt_int(1, 2);
std::cout << opt_int.add() << " " << opt_int.sub() << std::endl;
Operation<double> opt_double(1.4, 2.7);
std::cout << opt_double.add() << " " << opt_double.sub() << std::endl;
system("pause");
return 0;
}
C++类模板案例
488 views