c++(定义一个处理日期的类Date,它有3个私有数据成员:Year,Month,Day和若干个公有成员函数)

  1. 构造函数重载
  2. 定义一个成员函数printdate来打印日期;
  3. 定义一个非静态成员函数setdate来设置日期;
  4. 重载<<运算符,用于输出日期。
#include
using namespace std;
class Date
{
public:
	int day;
	int month;
	int year;
public:
	Date(int con_day, int con_month, int con_year); //构造函数
	Date(int con_day, int con_month);
	Date(int con_day);
	void printdate();
	void setdate();
	friend ostream &operator<<(ostream &os, const Date &a); //输出运算符重载
};
Date::Date(int con_day, int con_month, int con_year) //定义构造函数
{
	day = con_day;
	month = con_month;
	year = con_year;
}
Date::Date(int con_day, int con_month) //定义重载构造函数
{
	int con_year = 2018;
	day = con_day;
	month = con_month;
	year = con_year;
}
Date::Date(int con_day)  //定义重载构造函数
{
	int con_month = 10;
	int con_year = 2019;
	day = con_day;
	month = con_month;
	year = con_year;
}
void Date::printdate() //定义输出函数
{
	cout << "年:" << year << "月:" << month << "日:" << day << endl;
}
void Date::setdate() //定义输入函数
{
	cout << "请输入年,月,日";
	cin >> year;
	cin >> month;
	cin >> day;
}
ostream &operator<<(ostream &os, const Date &a) //定义输出运算符重载
{
	os << a.year << "年" << a.month << "月" << a.day << "日" << endl;
	return os;
}
int main()
{
	Date a1(15,3,2000);
	a1.printdate();
	Date a2(2,27);
	a2.printdate();
	Date a3(1);
	a3.printdate();
	Date a4(15, 3, 2000);
	a4.setdate();
	a4.printdate();
	cout << a1;
}

你可能感兴趣的:(c++(定义一个处理日期的类Date,它有3个私有数据成员:Year,Month,Day和若干个公有成员函数))