//---------main.cpp------------
#include <iostream>
#include <string>
#include <iomanip> //atoi()
using namespace std;
class Date
{
int year,month,day;
public:
void set(int y,int m ,int d)
{
year = y;
month = m;
day = d;
}
bool isLeapYeay();
void print();
void set(string s);
};
bool Date::isLeapYeay()
{
return (((year%4 == 0)&&(year%100 != 0))||(year%400 ==0));
}
inline void Date::print()
{
std::cout<<year<<"/"<<month<<"/"<<day<<" is a leap year!"<<endl;
}
void Date::set(string s)
{
year = atoi(s.substr(0,4).c_str()); //字符串转化成整形
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
}
int main(int argc,char **argv)
{
//-----------------对象----------------
Date s;
s.set(2012,9,25);
if(s.isLeapYeay()) s.print();
Date e;
e.set("2015-09-25");
e.print();
return 0;
}
set()函数根据参数类型不同而重载。。!
1