二、类
类是用户定义的数据类型:
例:
#include<iostream>
using namespace std;
class Date //class 是数据类型说明符,Date是所定义类型的名称
{
private:
int year;
int month;
int day;
public:
inline void init_date(int y, int m, int d); //内联函数
bool isLeapyear() {
if ((year % 4 == 0 && year % 100 != 0) || year % 400)
return true;
else
return false;
}
void print() {
cout << year << "-" << month << "-" << day << endl;
}
};
void Date::init_date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void main() {
Date today, yesterday, tomorrow;
today.init_date(2020, 2, 28);
yesterday.init_date(2020, 2, 27);
if (today.isLeapyear())
tomorrow.init_date(2020, 2, 29);
else<