Hello,大家好,我是爱吃香蕉的猴子,写写C++的小例子,顺便说一句:我是c++菜鸟,大神就不用看了;
c++使用面向对象,写闰年的判断
#include <iostream>
using namespace std;//命名空间
class Date
{
public://定义公用方法
void init(Date &d);
void print(Date & d);
bool isLeap(Date & d);
private://定义私用变量
int year;
int month;
int day;
};
void Date::init(Date &d) {//方法的实现
cout<< "year, month, dat:"<<endl;
cin>>d.year>>d.month>>d.day;//输入年月日
}
void Date::print(Date & d) {
cout<<"year month dat"<<endl;
cout<<d.year<<":"<<":"<<d.day<<endl;
}
bool Date::isLeap(Date &d) {
//闰年的判断条件:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是世纪闰年,2000年是世纪闰年)
if ((d.year%4 == 0 && d.year%100 != 0) || d.year%400 == 0) {
return true;
} else
return false;
}
int main() {
Date d;
d.init(d);
d.print(d);
if (d.isLeap(d)) {
cout<<"leap year"<<endl;
} else {
cout<<"not leap year"<<endl;
}
return 0;
}
Code的搬运工V1.0