C++之简单的类实现
1.功能实现:查询日期,一年中第几天。
#include<iostream>
using namespace std;
class Date
{
public: //成员函数一般共有,作用:操作成员变量
void steYear(int y);
void setMonth(int m);
void setDay(int d);
void setDate(int y, int m, int d); //修改年月日
void printDate(); //打印日期
bool isleap(); //判断是否为闰年
int numberOfDay(); //求第几天
private: //成员变量一般私有化
int year;
int month;
int day;
};
void Date::steYear(int y) {
year = y;
}
void Date::setMonth(int m) {
month = m;
}
void Date::setDay(int d) {
day = d;
}
void Date::setDate(int y, int m, int d){
//修改年月日
year = y;
month = m;
day = d;
}
void Date::printDate()
{
//打印日期
cout << year << "年" << month << "月" << day << "日";
}
bool Date::isleap()
{
//判断是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return true;
}
else
{
ret