#include <bits/stdc++.h>
using namespace std;
template<class T>
class AAA {
T a, b;
public:
AAA(T _a, T _b): a(_a), b(_b) {};
T sum() {
return a + b;
}
T cha();
};
template<class T>
T AAA<T>::cha() {
return a - b;
}//类外定义成员函数;
int main() {
int flag;
while (1) {
printf("flag=0输入结束\nflag=1:整数\nflag=2:浮点数\n");
printf("请输入flag的值:");
cin >> flag;
if (flag != 0) {
switch (flag) {
case 1: {
int a, b;
cout << "输入初始值(整型):" << endl;
cin >> a >> b;
AAA<int>s1(a, b);//类模板实例化
int flag1;
cout << "选择操作:1(求和)2(求差)" << endl;
cin >> flag1;
if (flag1 == 1) {
cout << a << "+" << b << "=" << s1.sum() << endl;
}
if (flag1 == 2) {
cout << a << "-" << b << "=" << s1.cha() << endl;
}
break;
};
case 2: {
float a, b;
cout << "输入初始值(浮点型):" << endl;
cin >> a >> b;
AAA<float>s2(a, b);//类模板实例化
int flag1;
cout << "选择操作:1(求和)2(求差)" << endl;
cin >> flag1;
if (flag1 == 1) {
cout << a << "+" << b << "=" << s2.sum() << endl;
}
if (flag1 == 2) {
cout << a << "-" << b << "=" << s2.cha() << endl;
}
break;
}
}
} else {
cout << "输入结束!" << endl;
break;
}
}
return 0;
}