源程序:
#include <iostream>
#include <string>
using namespace std;
class myDate
{
private:
int year, month, day;
public:
myDate();
myDate(int); //默认参数,从右向左初始化
myDate(int, int);
myDate(int, int, int);
void show();
};
myDate::myDate() :year(1990), month(9), day(19) {}
myDate::myDate(int y, int m) : day(25)
{
year = y;
month = m;
}
myDate::myDate(int y) : month(12), day(11)
{
year = y;
}
myDate::myDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
void myDate::show()
{
cout << "year:" << year << "month:" << month << "day:" << day << endl;
}
int main()
{
myDate M;
myDate N(1982);
myDate P(1992, 5);
myDate Q(2001, 11, 11);
cout << endl;
M.show();
N.show();
P.show();
Q.show();
system("pause");
return 1;
}
运行结果: