|
20鱼币
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<class T1,class T2>
class people
{
public:
people();
people(const T1&x,const T2&y);
~people();
friend ostream&operator<<(ostream&x,const people&y);
private:
T1 name;
T2 age;
};
template<class T1,class T2>
people<T1,T2>::people():name("默认构造函数创建一个新对象"),age(1){}
template<class T1,class T2>
people<T1,T2>::~people(){cout << "析构people.\n";}
template<class T1,class T2>
ostream&operator<<(ostream&x,const people<T1,T2>&y)
{
x<<y.name << "的年龄:" << y.age << endl;
return(x);
}
template<class T1,class T2>
people<T1,T2>::people(const T1&x,const T2&y):name(x),age(y){}
template<class T1>
void show(const vector<T1>&x)
{
for(int i=0;i<x.size();i++)
{
cout << x[i] << endl;
}
cout << endl;
}
int main()
{
vector<people<string,int>>one(1);
show(one);
system("pause");
}
|
|