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

📄 date.cpp

📁 本程序给出了SSD5实验课中Exercise6的参考程序 内附有详细的注释 下载后请仔细参照源代码进行分析 切莫完全搬照
💻 CPP
字号:
#include<iostream>
#include"Date.h"

using namespace std;

/* Initializes the private data members to default values */
Date::Date(void):month(0),day(0),year(0),hour(0),minute(0),second(0){}

/* Initializes the private data members day, month, year, hour, minute, and second */
Date::Date (int month, int day, int year, int hour, int minute, int second):
month(month),day(day),year(year),hour(hour),minute(minute),second(second){}

/* Change the value of "month" */
void Date::setMonth(int &month){
   this ->month = month;
}  

/* Change the value of "day" */ 
void Date::setDay(int &day){
	this ->day = day;
}

/* Change the value of "year" */
void Date::setYear(int &year){
	this ->year = year;
}

/* Change the value of "hour" */
void Date::setHour(int &hour){
	this ->hour = hour;
}

/* Change the value of "minute" */
void Date::setMinute(int &minute){
	this ->minute = minute;
}

/* Change the value of "second" */
void Date::setSecond(int &second){
	this ->second = second;
}

/* Get the value of "month" */
int Date::getMonth(void) const{
    return month;
}

/* Get the value of "day" */	    
int Date::getDay(void) const{
	return day;
}  
 
/* Get the value of "year" */
int Date::getYear(void) const{
	return year;
}
	
/* Get the value of "hour" */   
int Date::getHour(void) const{
    return hour;
}

/* Get the value of "minute" */	    
int Date::getMinute(void) const{
	return minute;
}

/* Get the value of "second" */	    
int Date::getSecond(void) const{
	return second;
}
	 
/* Compares Date objects for equality */
bool Date::operator== (const Date &rhs){
	if(month == rhs.getMonth() && day == rhs.getDay() && year == rhs.getYear() 
		&& hour == rhs.getHour() && minute == rhs.getMinute() && second == rhs.getSecond()){
		return true;
	}
	else 
		return false;
}

/* Compares Date objects, returns true if the invoking Date
 * object is less than the other Date object */
bool Date::operator< (const Date &rhs){
	if(year < rhs.getYear()){
		return true;
	}
	else if(year == rhs.getYear() && month < rhs.getMonth()){
		return true;
	}
	else if (year == rhs.getYear() && month == rhs.getMonth() && day < rhs.getDay()){
		return true;
	}
	else if (year == rhs.getYear() && month == rhs.getMonth() && day == rhs.getDay() 
		&& hour < rhs.getHour()){
		return true;
	}
	else if (year == rhs.getYear() && month == rhs.getMonth() && day == rhs.getDay() 
		&& hour == rhs.getHour() && minute < rhs.getMinute()){
		return true;
	}
	else if(year == rhs.getYear() && month == rhs.getMonth() && day == rhs.getDay() 
		&& hour == rhs.getHour() && minute == rhs.getMinute() && second < rhs.getSecond()){
		return true;
	}
	else 
		return false;

}

/* Outputs a Date object to an output stream in the form "mm/dd/yyyy hh:nn:ss" */
ostream &operator<<(ostream& stream, const Date& date){
	stream << date.getMonth() << "/" << date.getDay() << "/" << date.getYear() << "/" <<
		" " << date.getHour() << ":" << date.getMinute() << ":" << date.getSecond() << endl;
	return stream;
}

/* This operator reads a Date object from an input stream */
istream &operator>>(istream& stream, Date& date){
	int b;
	char a; 

	stream >> b;
	date.setMonth(b);

	stream >> a;
	stream >> b;
	date.setDay(b);

	stream >> a;
	stream >> b;
	date.setYear(b);

	stream >> b;
	date.setHour(b);

	stream >> a;
	stream >> b;
	date.setMinute(b);

	stream >> a;
	stream >> b;
	date.setSecond(b);
	stream >> a;

	return stream;
	
}

⌨️ 快捷键说明

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