cpp03.cpp

来自「C++参考书」· C++ 代码 · 共 72 行

CPP
72
字号

// Coded by plusir -- Jan.07.2003.
// Standard C++ Bible -- (P363-12-3)

#include <iostream>
using namespace std ;

class Date
{
	public:
		Date( int m = 0, int d = 0, int y = 0 )
		{
			month = m ;
			day = d ;
			year = y ;
		}

		void display( void ) const
		{
			cout << month << '/' << day << '/' << year ;
		}

		bool operator == ( Date & ) const ;
		bool operator < ( Date & ) const ;

	private:
		int month ;
		int day ;
		int year ;

		static int dys[] ;
} ;

int Date::dys[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;

bool Date::operator == ( Date &dt ) const
{
	return ( this->month == dt.month && this->day == dt.day && this->year == dt.year ) ;
}

bool Date::operator < ( Date &dt ) const
{
	if ( this->year == dt.year ) {
		if ( this->month == dt.month )
			return this->day < dt.day ;
		return this->month < dt.month ;
	}
	return this->year < dt.year ;
}

int main()
{
	Date date1( 12, 7, 1941 ),
		 date2( 2, 22, 1990 ),
		 date3( 12, 7, 1941 ) ;

	if ( date1 < date2 ) {
		date1.display() ;
		cout << " is less than " ;
		date2.display() ;
	}
	cout << endl ;

	if ( date1 == date3 ) {
		date1.display() ;
		cout << " is equal to " ;
		date3.display() ;
	}

	return 0 ;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?