用多个类文件定义一个类DDate,内有数据成员year、month和day,分别代表年、月、日,并若干有成员函数:构造函数用于初始化数据成员,isLeap函数用于闰年的判断。编写主函数:创建日期对象,判断该年是否是闰年。
时间: 2023-05-30 08:06:36 浏览: 247
DDate.h文件:
```c++
#pragma once
class DDate
{
public:
DDate(int y, int m, int d); //构造函数
bool isLeap(); //判断是否是闰年
private:
int year;
int month;
int day;
};
```
DDate.cpp文件:
```c++
#include "DDate.h"
DDate::DDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
bool DDate::isLeap()
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
```
main.cpp文件:
```c++
#include <iostream>
#include "DDate.h"
using namespace std;
int main()
{
DDate date(2020, 1, 1);
if (date.isLeap())
cout << date.year << " is a leap year." << endl;
else
cout << date.year << " is not a leap year." << endl;
return 0;
}
```
输出:
```
2020 is a leap year.
```
阅读全文