//定义2个人,2个人相加,得到1个人,年龄是之和,名字用第1个人的
#include< iostream >
using namespace std;
template< class T >
T add(T a,T b){
return a + b;
}
class Person{
private:
string name;
int age;
public:
Person(){
}
Person(string n,int a){
name = n;
age = a;
}
void print(){
cout << "姓名: " << name << " 年龄:"<<age << endl;
}
Person operator+(Person p){
return Person(name,age + p.age);
}
};
//template int add(int,int); //用int来显式实例化
int main(){
Person p1("张三",17);
Person p2("李四",20);
Person p;
p = add(p1,p2);
p.print();
cout << add(10,20) << endl;
return 0;
}