自考新教材-p332

源程序:

#include <iostream>
using namespace std;
template<class T> //类类型T
void Swap(T &x, T &y) //可以交换类对象
{
T tmp = x;
x = y;
y = tmp;
}
class myDate
{
public:
myDate();
myDate(int, int, int);
void printDate() const;
private:
int year, month, day;
};
myDate::myDate()
{
year = 1970;
month = 1;
day = 1;
}
myDate::myDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
void myDate::printDate() const
{
cout << year << "/" << month << "/" << day;
return;
}
int main()
{
int n = 1, m = 2;
Swap(n, m);
double f = 1.2, g = 2.3;
Swap(f, g);
myDate d1, d2(2000, 1, 1);
Swap(d1, d2);
system("pause");
d1.printDate();
cout << endl;
d2.printDate();
system("pause");
return 0;
}

运行结果:

 

posted @ 2020-02-05 20:13  bobo哥  阅读(137)  评论(0编辑  收藏  举报