⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp18.cpp

📁 C++参考书
💻 CPP
字号:

// Coded by plusir -- Dec.30.2002.
// Standard C++ Bible - (P312-11-18)

#include <iostream>
#include <string>
using namespace std ;

class Date
{
	public:
		Date( int = 1, int = 1, int = 1900 ) ;
		~Date( void ) ;
		void display( void ) const ;
		void operator = ( const Date & ) ;

	private:
		char *month ;
		int day ;
		int year ;
		int mo ;

		static char *monthName[12] ;
} ;

char* Date::monthName[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ;

Date::Date( int m, int d, int y )
{
	if ( m >=1 && m <=12 ) {
		month = new char[strlen( monthName[m - 1] ) + 1] ;
		strcpy( month, monthName[m - 1] ) ;
		mo = m ;
	}
	else {
		month = NULL ;
		mo = 0 ;
	}

	day = d ;
	year = y ;
}

Date::~Date( void )
{
	delete [] month ;
}

void Date::display( void ) const
{
	cout << month << ' ' << day << ", " << year << endl ;
}

void Date::operator = ( const Date &d )
{
	if ( this == &d )
		return ;

	if ( d.month != NULL ) {
		delete [] month ;
		month = new char[strlen( monthName[d.mo - 1] ) + 1] ;
		strcpy( month, monthName[d.mo - 1] ) ;

		mo = d.mo ;
	}
	else {
		month = NULL ;
		mo = 0 ;
	}

	day = d.day ;
	year = d.year ;
}

int main()
{
	Date birthday( 6, 24, 1940 ) ;
	birthday.display() ;

	Date newDay( 7, 29, 1941 ) ;
	newDay.display() ;

	newDay = birthday ;
    newDay.display() ;

	return 0 ;
}

⌨️ 快捷键说明

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