源程序:
#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 *pM=new myDate();
myDate *pN=new myDate(1982);
myDate *pP=new myDate(1992, 5);
myDate *pQ=new myDate(2001, 11, 11);
cout << endl;
pM->show();
pN->show();
pP->show();
pQ->show();
system("pause");
return 1;
}
运行结果: