CPT106 C++ Programming and Software Engineering II Individual Project Fundamental of class and obj

本文详细描述了一个C++编程项目,涉及创建一个日期类MyDate,包含年、月、日成员,以及使用运算符重载实现日期比较、加减等操作。同时介绍了子类SubDate,扩展了日期和时间信息。在主函数中,用户输入并测试了这两个类的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CPT106 C++ Programming and Software Engineering II

Individual Project

Fundamental of class and object

Write a C++ program that can perform the following:

  1. Create a class MyDate to represent a date like follows:
    25-JAN-2016
    The class contains three data members, denoting the year, the 3-letter abbreviation of the month
    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) and the date (Note that it is
    assumed that, for this assignment, Feb. has 28 days.).
    a. Define constructors and destructors of this class
    b. Define members to return the date, year, month and day, respectively.
    c. Using “operator overloading” to realise the following tasks:
  1. Define the assignment operator “=” to assign the value of one Date object to the
    other.
  2. Define the comparator “>” “<” and “==” to compare two dates.
  3. Define the unary sign “++” to add one day to a date object.
  4. Define the subtractor “-” to subtract days from a date object and return a new date
    object.
  1. Create a new subclass SubDate, which inherits from the class MyDate and represents a new
    date like follows:
    25-JAN-2016 16:23:18
    a. Define new data members of the subclass to represent the hour, minute and second.
    b. Define constructors of the subclass.
    c. Define members to return the date (day-month-year HH:mm:ss), time (HH:mm:ss),
    hour, minute and second, respectively.
    d. Define the subtractor “-” in the sub class to return the number of days between a
    SubDate object and a MyDate object.
    For example:
    SubDate d1(2016, JAN, 25, 18, 23,50), d2(2016, JAN,18); /two MyDate objects/
    int days=d1-d2; /days=7/
  2. In the main function, you should test these two classes following the requirements below:
    Requirements:
    a. Create objects of these two classes in your main function.
    b. The value of objects should be inputted from the keyboard,
    c. Test all the function members of the two classes in the main function, and print out the
    results of these function members

#include "SubDate.cpp"
#include <iostream>
using namespace std;
// a.Create objects of these two classes in your main function.
// b.The value of objects should be inputted from the keyboard,
// c.Test all the function members of the two classes in the main function, and print out the
// results of these function members.
int main() {
	int year, day, hour, minute, second;
	string month;
	cout << "MyDate \n" <<"year: " << endl;
	cin >> year;
	cout << "month: " << endl;
	cin >> month;
	cout << "day: " << endl;
	cin >> day;
	cin.get();
	MyDate md = MyDate(year, month, day);

	cout << "test MyDate" << endl;

	cout << "test1: int get_day()" << endl;
	cout << md.get_day() << endl;

	cout << "test2: string get_month()" << endl;
	cout << md.get_month() << endl;

	cout << "test3: int get_year()" << endl;
	cout << md.get_year() << endl;

	cout << "test4: MyDate& operator=(const MyDate & d)" << endl;
	MyDate md2 = MyDate();
	md2 = md;
	cout << md2.get_day() << " - ";
	cout << md2.get_month() << " - ";
	cout << md2.get_year() << endl;

	cout << "test5: bool operator>(const MyDate & d)" << endl;
	cout << (md > md2) << endl;

	cout << "test6: bool operator<(const MyDate & d)" << endl;
	cout << (md < md2) << endl;

	cout << "test7: bool operator==(const MyDate & d)" << endl;
	cout << (md == md2) << endl;

	cout << "test8: MyDate operator++(int) md2++;" << endl;
	cout << md2.get_day() << " - ";
	cout << md2.get_month() << " - ";
	cout << md2.get_year() << endl;
	md2++;
	cout << md2.get_day() << " - ";
	cout << md2.get_month() << " - ";
	cout << md2.get_year() << endl;

	cout << "test9: MyDate operator-(int day) md2 = md2 - 100;" << endl;
	cout << md2.get_day() << " - ";
	cout << md2.get_month() << " - ";
	cout << md2.get_year() << endl;
	md2 = md2 - 100;
	cout << md2.get_day() << " - ";
	cout << md2.get_month() << " - ";
	cout << md2.get_year() << endl;
	

	cout << "SubDate\n " << "year: " << endl;
	cin >> year;
	cout << "month: " << endl;
	cin >> month;
	cout << "day: " << endl;
	cin >> day;
	cout << "hour: " << endl;
	cin >> hour;
	cout << "minute: " << endl;
	cin >> minute;
	cout << "second: " << endl;
	cin >> second;
	SubDate sd = SubDate(year, month, day, hour, minute, second);

	cout << "test SubDate" << endl;
	cout << "test1: void get_date()" << endl;
	sd.get_date();

	cout << "test2: void get_time()" << endl;
	sd.get_time();

	cout << "test3: int get_hour()" << endl;
	cout << sd.get_hour() << endl;

	cout << "test4: int get_minute()" << endl;
	cout << sd.get_minute() << endl;

	cout << "test5: int get_second()" << endl;
	cout << sd.get_second() << endl;

	cout << "test6: int operator-(MyDate d)" << endl;
	cout << "SubDate: ";
	sd.get_date();
	cout << "MyDate: ";
	cout << md.get_day() << " - ";
	cout << md.get_month() << " - ";
	cout << md.get_year() << endl;
	cout << " = " << (sd - md) << endl;

	return 0;
}

MyDate.cpp

// 1. Create a class MyDate to represent a date like follows :
// 25 - JAN - 2016

#include <string>
using namespace std;

class MyDate {
private:
	int year;
	string month;
	int mon;
	int day;

public:
	// constructors and destructors
	MyDate(int year, string month, int day)
	{
		this->year = year;
		this->month = month;
		this->day = day;
		string m[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
			"Aug", "Sep", "Oct", "Nov", "Dec" };
		for (int i = 0; i < 12; i++) {
			if (m[i] == month) {
				this->mon = i + 1;
			}
		}
	};
	MyDate()
	{
		this->year = 0;
		this->month = "No";
		this->mon = 0;
		this->day = 0;
	};
	~MyDate() {};

	// members
	int get_day() {
		return this->day;
	};
	string get_month() {
		return this->month;
	};
	int get_year() {
		return this->year;
	};

	// operator overloading
	
	//  1) assign the value of one Date object to the other.
	MyDate& operator=(const MyDate& d) {
		if (this != &d)
		{
			this->year = d.year;
			this->month = d.month;
			this->mon = d.mon;
			this->day = d.day;
		}
		return *this;
	};

	//  2) Define the comparator “ > ” “ < ” and “ == ” to compare two dates.
	bool operator>(const MyDate& d) {
		// compare from year to month
		if (this->year > d.year)
		{
			return true;
		}
		else if ((this->year == d.year) && (this->mon > d.mon))
		{
			return true;
		}
		else if ((this->year == d.year) && (this->mon == d.mon) && (this->day > d.day))
		{
			return true;
		}
		else
		{
			return false;
		}
	};
	bool operator<(const MyDate& d) {
		// if not > and == is <
		return !((*this > d) || (*this == d));
	};
	bool operator==(const MyDate& d) {
		return (this->year == d.year && this->month == d.month && this->day == d.day);
	};

	//	3) Define the unary sign “++” to add one day to a date object.
	MyDate operator++(int) {
		string m[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
			"Aug", "Sep", "Oct", "Nov", "Dec" };
		int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		this->day++;
		while (this->day > days[this->mon])
		{
			this->day -= days[this->mon];
			this->mon++;
			if (this->mon == 13)
			{
				this->year++;
				this->mon = 1;
			}
		}
		this->month = m[this->mon - 1];
		return *this;
	};

	//	4) Define the subtractor “ - ” to subtract days from a date object and return a new date object.
	MyDate operator-(int day) {
		string m[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
			"Aug", "Sep", "Oct", "Nov", "Dec" };
		int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // number of days in month
		MyDate tmp = *this; // change on tmp
		if (day < 0)
		{
			tmp.day -= day;
			while (tmp.day > days[tmp.mon])
			{
				tmp.day -= days[tmp.mon];
				tmp.mon++;
				if (tmp.mon == 13)
				{
					tmp.year++;
					tmp.mon = 1;
				}
			}
			tmp.month = m[tmp.mon - 1];
			return tmp;
		}

		tmp.day -= day;
		while (tmp.day <= 0)
		{
			tmp.mon--;
			if (tmp.mon == 0)
			{
				tmp.year--;
				tmp.mon = 12;
			}
			tmp.day += days[tmp.mon];
		}
		tmp.month = m[tmp.mon - 1];
		return tmp;

	};
};


SubDate.cpp

// 2. Create a new subclass SubDate, which inherits from the class MyDate and represents a new
// date like follows : 25 - JAN - 2016 16 : 23 : 18
// a.Define new data members of the subclass to represent the hour, minute and second.
// b.Define constructors of the subclass.
// c.Define members to return the date(day - month - year HH : mm:ss), time(HH : mm : ss),
// hour, minute and second, respectively.
// d.Define the subtractor "-" in the sub class to return the number of days between a
// SubDate object and a MyDate object.
// For example :
// SubDate d1(2016, JAN, 25, 18, 23, 50), d2(2016, JAN, 18); /*two MyDate objects*/
// int days = d1 - d2; /*days=7*/
#include "MyDate.cpp"

class SubDate : public MyDate {
private:
	int hour;
	int minute;
	int second;
public:
	SubDate(int hour, int minute, int second) {
		this->hour = hour;
		this->minute = minute;
		this->second = second;
	};
	SubDate(int year, string month, int day, int hour, int minute, int second)
		:MyDate(year, month, day), hour(hour), minute(minute), second(second)
	{};
	~SubDate() {
	};
	void get_date() {
		printf("%02d - %s - %04d %02d : %02d : %02d\n", get_day(), get_month().c_str(), get_year(), hour, minute, second);
	};
	void get_time() {
		printf("%02d : %02d : %02d\n", hour, minute, second);
	};
	int get_hour() {
		return this->hour;
	};
	int get_minute() {
		return this->minute;
	};
	int get_second() {
		return this->second;
	};
	// return the number of days between a SubDate object and a MyDate object.
	int operator-(MyDate d) {

		int count = 0;
		if (*this > d)
		{
			while (!(*this == d)) // add d until tmp == d
			{
				d++;
				count++;
			}
		}
		else
		{
			while (!(*this == d)) // sub d until tmp == d
			{
				d = d - 1;
				count--;
			}
		}
		return count;
	}; 
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值