自考新教材-p350_2

源程序:

#include <iostream>
using namespace std;
template <class T>

class A
{
private:
T x, y, s;
public:
A(T a, T b)
{
x = a;
y = b;
s = x + y;
}
void show()
{
cout << "x+y=" << s << endl;
}
};

int main()
{
A <int>add(10,100);
add.show();
A <float>add1(1.1,2);
add1.show();
system("pause");
return 1;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12266896.html