源程序:

#include <iostream>

using namespace std;

class myDate

{

public:

myDate();

myDate(int,int,int);

void setDate(int,int,int);

void setDate(myDate);

myDate getDate();

void setYear(int);

int getMonth();

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::setDate(int y, int m, int d)

{

year = y;

month = m;

day = d;

return;

}

void myDate::setDate(myDate oneD)

{

year = oneD.year;

month = oneD.month;

day = oneD.day;

return;

}

myDate myDate::getDate()

{

return *this;

}

void myDate::setYear(int y)

{

year = y;

return;

}

int myDate::getMonth()

{

return month;

}

void myDate::printDate() const

{

cout << year << "/" << month << "/" << day;

return;

}

 

int main()

{

myDate A;

myDate M;

myDate N(1995,8,30);

M.setDate(2001,12,23);

M.printDate();

cout << endl;

N.printDate();

cout << endl;

M.setDate(A);

A.printDate();

cout << endl;

system("pause");

return 0;

}

 运行结果:

自考新教材--p69_成员函数