一、第一关:对象创建与赋值
Date.cpp
/************************************ Copyright(c) 2016-2099 CTGU YOUNGMIEN* All rights reserved.#pragma once* Version: v1.0* Author: YOUNGMIEN* Date: 2020/4/8* Company: CTGU** fileName: Date.cpp* Descripton: Task File 任务文件********************************//*Name: Date.cppDate: 07/04/20 10:09Description: 任务文件第1关需要完成//设置日期void reset();void set(int,int,int);void set(string);void show();第2关需要完成//构造函数//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);void show();第3关需要完成string toString();//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);KiKi学习了面向对象技术,学会了通过封装属性(变量)和行为(函数)定义类,现在他要设计一个电子日历类TDate。它有3个私有数据成员:m_month,m_day,m_year和若干个公有成员函数,要求:(1)带有默认形参值的构造函数,默认值为0, 0, 0;(2)输出日期函数,用“日/月/年”格式输出日期;(3)设置日期函数,从键盘输入年、月、日。输入描述:一行,三个整数,用空格分隔,分别表示年、月、日。输出描述:一行,用"日/月/年"格式输出日期。示例1输入2019 12 30输出30/12/2019*//*PART 2:类的实现部分通过类作用域标识符::来指明成员函数是属于哪一个类*/#include "Date.h"#include "comm.h"#include <iomanip>void Date::reset() {//第1关需要完成/********** Begin ***********/cout << "reset date to default!" << endl;this->m_year = 2000;this->m_month = 1;this->m_day = 1;return;/********** End ***********/}//将日期设置为输入的year,month,dayvoid Date::set(int year, int month, int day) {//第1关需要完成/********** Begin ***********/cout << "set date to the value you specified!" << endl;this->m_year = year;this->m_month = month;this->m_day = day;/********** End ***********/}//将日期设置为输入的string,格式为:yyyy-mm-dd 2020-04-01void Date::set(string s) {//第1关需要完成/********** Begin ***********/cout << "set date to the value from string!" << endl;string year = s.substr(0, 4);string month = s.substr(5, 2);string day = s.substr(8, 2);this->m_year = atoi(year.c_str());this->m_month = atoi(month.c_str());this->m_day = atoi(day.c_str());return;/********** End ***********/}//构造函数 ,初始化 第2, 3关需要完成/*无参:default假定日期为2000-1-1*/Date::Date(){/********** Begin ***********//********** End ***********/}/*@param y,m,d: int ,年,月,日*/Date::Date(int y,int m ,int d){/********** Begin ***********//********** End ***********/}//copy constructor,参数为同类对象常引用// Parameters are const referenced by same objectDate::Date(const Date& objref){/********** Begin ***********//********** End ***********/}//输出:格式为日,月,年. 2020年4月7日 07/04/2020void Date::show(){//第1关需要完成第2关需要完成/********** Begin ***********/cout << "dd/mm/yyyy: ";// cout << this->m_day << "/" << this->m_month << "/" << this->m_year << endl;cout << setw(2) << setfill('0') << this->m_day << '/' << setw(2) << setfill('0')<< this->m_month << '/' << setw(2) << setfill('0') << this->m_year << endl;/********** End ***********/}//将Date对象转换为string对象 第3关需要完成string Date:: toString(){string to_str="";/********** Begin ***********//********** End ***********/return to_str;}
二、第二关:构造函数
Date.cpp
/************************************ Copyright(c) 2016-2099 CTGU YOUNGMIEN* All rights reserved.#pragma once* Version: v1.0* Author: YOUNGMIEN* Date: 2020/4/8* Company: CTGU** fileName: Date.cpp* Descripton: Task File 任务文件********************************/#include "comm.h"/*Name: Date.cppDate: 07/04/20 10:09Description: 任务文件第1关需要完成//设置日期void reset();void set(int,int,int);void set(string);void show();第2关需要完成//构造函数//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);void show();第3关需要完成string toString();//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);KiKi学习了面向对象技术,学会了通过封装属性(变量)和行为(函数)定义类,现在他要设计一个电子日历类TDate。它有3个私有数据成员:m_month,m_day,m_year和若干个公有成员函数,要求:(1)带有默认形参值的构造函数,默认值为0, 0, 0;(2)输出日期函数,用“日/月/年”格式输出日期;(3)设置日期函数,从键盘输入年、月、日。输入描述:一行,三个整数,用空格分隔,分别表示年、月、日。输出描述:一行,用"日/月/年"格式输出日期。示例1输入2019 12 30输出30/12/2019*//*PART 2:类的实现部分通过类作用域标识符::来指明成员函数是属于哪一个类*/#include "Date.h"//设置日期//将日期重置为2000年1月1日void Date::reset() {//第1关需要完成/********** Begin ***********/cout << "reset date to default!" << endl;this->m_year = 2000;this->m_month = 1;this->m_day = 1;return;/********** End ***********/}//将日期设置为输入的year,month,dayvoid Date::set(int year, int month, int day) {//第1关需要完成/********** Begin ***********/cout << "set date to the value you specified!" << endl;this->m_year = year;this->m_month = month;this->m_day = day;/********** End ***********/}//将日期设置为输入的string,格式为:yyyy-mm-dd 2020-04-01void Date::set(string s) {//第1关需要完成/********** Begin ***********/cout << "set date to the value from string!" << endl;string year = s.substr(0, 4);string month = s.substr(5, 2);string day = s.substr(8, 2);this->m_year = atoi(year.c_str());this->m_month = atoi(month.c_str());this->m_day = atoi(day.c_str());return;/********** End ***********/}//构造函数 ,初始化 第2, 3关需要完成/*无参:default假定日期为2000-1-1*/Date::Date(){/********** Begin ***********/cout << "constructor default!" << endl;this->m_year = 2000;this->m_month = 1;this->m_day = 1;/********** End ***********/}/*@param y,m,d: int ,年,月,日*/Date::Date(int y,int m ,int d){/********** Begin ***********/cout << "constructor with 3 args!" << endl;this->m_year = y;this->m_month = m;this->m_day = d;/********** End ***********/}//copy constructor,参数为同类对象常引用// Parameters are const referenced by same objectDate::Date(const Date& objref){/********** Begin ***********/cout << "copy constructor !" << endl;this->m_year = objref.m_year;this->m_month = objref.m_month;this->m_day = objref.m_day;/********** End ***********/}//输出:格式为日,月,年. 2020年4月7日 07/04/2020void Date::show(){//第1关需要完成第2关需要完成/********** Begin ***********/cout << "dd/mm/yyyy: ";// cout << this->m_day << "/" << this->m_month << "/" << this->m_year << endl;cout << setw(2) << setfill('0') << this->m_day << '/' << setw(2) << setfill('0')<< this->m_month << '/' << setw(2) << setfill('0') << this->m_year << endl;/********** End ***********/}//将Date对象转换为string对象 第3关需要完成string Date:: toString(){string to_str="";/********** Begin ***********//********** End ***********/return to_str;}
三、第三关:toString——对象转string
Date.cpp
/************************************ Copyright(c) 2016-2099 CTGU YOUNGMIEN* All rights reserved.#pragma once* Version: v1.0* Author: YOUNGMIEN* Date: 2020/4/8* Company: CTGU** fileName: Date.cpp* Descripton: Task File 任务文件********************************/#include "comm.h"/*Name: Date.cppDate: 07/04/20 10:09Description: 任务文件第1关需要完成//设置日期void reset();void set(int,int,int);void set(string);void show();第2关需要完成//构造函数//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);void show();第3关需要完成string toString();//constructor defaultDate();//constructor with argsDate(int,int,int);//copy constructorDate(const Date&);KiKi学习了面向对象技术,学会了通过封装属性(变量)和行为(函数)定义类,现在他要设计一个电子日历类TDate。它有3个私有数据成员:m_month,m_day,m_year和若干个公有成员函数,要求:(1)带有默认形参值的构造函数,默认值为0, 0, 0;(2)输出日期函数,用“日/月/年”格式输出日期;(3)设置日期函数,从键盘输入年、月、日。输入描述:一行,三个整数,用空格分隔,分别表示年、月、日。输出描述:一行,用"日/月/年"格式输出日期。示例1输入2019 12 30输出30/12/2019*//*PART 2:类的实现部分通过类作用域标识符::来指明成员函数是属于哪一个类*/#include "Date.h"//设置日期//将日期重置为2000年1月1日void Date::reset() {//第1关需要完成/********** Begin ***********/cout << "reset date to default!" << endl;this->m_year = 2000;this->m_month = 1;this->m_day = 1;return;/********** End ***********/}//将日期设置为输入的year,month,dayvoid Date::set(int year, int month, int day) {//第1关需要完成/********** Begin ***********/cout << "set date to the value you specified!" << endl;this->m_year = year;this->m_month = month;this->m_day = day;/********** End ***********/}//将日期设置为输入的string,格式为:yyyy-mm-dd 2020-04-01void Date::set(string s) {//第1关需要完成/********** Begin ***********/cout << "set date to the value from string!" << endl;string year = s.substr(0, 4);string month = s.substr(5, 2);string day = s.substr(8, 2);this->m_year = atoi(year.c_str());this->m_month = atoi(month.c_str());this->m_day = atoi(day.c_str());return;/********** End ***********/}//构造函数 ,初始化 第2, 3关需要完成/*无参:default假定日期为2000-1-1*/Date::Date(){/********** Begin ***********/cout << "constructor default!" << endl;this->m_year = 2000;this->m_month = 1;this->m_day = 1;/********** End ***********/}/*@param y,m,d: int ,年,月,日*/Date::Date(int y,int m ,int d){/********** Begin ***********/cout << "constructor with 3 args!" << endl;this->m_year = y;this->m_month = m;this->m_day = d;/********** End ***********/}//copy constructor,参数为同类对象常引用// Parameters are const referenced by same objectDate::Date(const Date& objref){/********** Begin ***********/cout << "copy constructor !" << endl;this->m_year = objref.m_year;this->m_month = objref.m_month;this->m_day = objref.m_day;/********** End ***********/}//输出:格式为日,月,年. 2020年4月7日 07/04/2020void Date::show(){//第1关需要完成第2关需要完成/********** Begin ***********/cout << "dd/mm/yyyy: ";// cout << this->m_day << "/" << this->m_month << "/" << this->m_year << endl;cout << setw(2) << setfill('0') << this->m_day << '/' << setw(2) << setfill('0')<< this->m_month << '/' << setw(2) << setfill('0') << this->m_year << endl;/********** End ***********/}//将Date对象转换为string对象 第3关需要完成string Date:: toString(){string to_str="";/********** Begin ***********/to_str = to_str + to_string(this->m_day);to_str += '/';to_str += to_string(this->m_month);to_str += '/';to_str += to_string(this->m_year);/********** End ***********/return to_str;}
