源程序:
#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 A[4] = {myDate(),myDate(1982), myDate(1992,5), myDate(2001,11,11)};
myDate *p = A; //用指针指向数据的方法
p[0].show();
p[1].show();
p[2].show();
p[3].show();
system("pause");
return 1;
}
运行结果: