参考:面向对象程序设计及C++,P60
共3个文件
// excise_20230808d.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "li03_08.h"
int main()
{
CDate array[3] =
{ CDate(2019,3,8), CDate(2019,3,11) };//array[0] = CDate(2019,3,8)
for (int i = 0; i < 3; i++)
array[i].Display();
std::cout << "Hello World!\n";
getchar();
}
#pragma once
#include <iostream>
using namespace std;
class CDate
{
private:
int Date_Year;
int Date_Month;
int Date_Day;
public:
CDate(int y = 2000, int m = 1, int d = 1); //带有默认参数的构造函数
void Display();
};
#include "li03_08.h"
CDate::CDate(int y, int m, int d) //带有默认参数的构造函数
{
cout << "执行初始化" << endl;
Date_Year = y;
Date_Month = m;
Date_Day = d;
}
void CDate::Display()
{
cout << Date_Year << "-" << Date_Month << "-" << Date_Day << endl;
}
运行结果: