日期类

实现一个日期类,包含缺省值的构造函数、赋值操作符的重载、对==的重载


#include 
using namespace std;

class Date
{
private:
	int _year;
	int _month;
	int _day;
public:
	void Display()
	{
		cout << "" << this << endl;
		cout << this->_year << "-" << this->_month << "-" << this->_day << endl;
	}
	Date(int year = 1900, int month = 1, int day = 1)
	{
		cout << "Date(int year = 1900, int month = 1, int day = 1)" << endl;
		_year = year;
		_month = month;
		_day = day;
	}

	bool operator==(const Date& d)
	{
		return _year == d._year&&_month == d._month &&_day == d._day;
	}
	Date& operator=(const Date& d)
	{
		cout << "赋值运算符的重载" << endl; 
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
};

int main()
{
	Date d1(2015,8,23);
	d1.Display();
	Date d2(2016, 8, 23);
	d2.Display();
	Date d3(d2);

	cout << "d1==d2?" << (d1 == d2) << endl;
	cout << "d2==d3?" << (d2 == d3) << endl;

	getchar();
	return 0;
}


你可能感兴趣的:(控制台,C++)