c/c++语言开发共享C++类和对象实战之Date类的实现方法

目录零、前言一、date类相关接口二、具体接口函数实现1、获取月份天数2、date打印3、date构造函数4、date析构函数5、date拷贝构造函数6、date赋值重载函数7、date+=天数8、d

目录
  • 零、前言
  • 一、date类相关接口
  • 二、具体接口函数实现
    • 1、获取月份天数
    • 2、date打印
    • 3、date构造函数
    • 4、date析构函数
    • 5、date拷贝构造函数
    • 6、date赋值重载函数
    • 7、date+=天数
    • 8、date+天数
    • 9、date-=天数
    • 10、date-天数
    • 11、++date
    • 12、date++
    • 13、–date
    • 14、date–
    • 15、日期比较
    • 16、date相减
    • 17、日期输入日期输出
  • 总结

    零、前言

    在学了c++类和对象基本知识以及六个默认成员函数后,我们可以上手实现一个date类出来,检验学习的效果。

    一、date类相关接口

    接口展示:

      class date  {   	//输出操作符重载  	friend ostream& operator<<(ostream& _cout, const date& d);  	//输出操作符重载  	friend istream& operator>>(istream& _cin, date& d);    public:      // 获取某年某月的天数      int getmonthday(int year, int month);        // 全缺省的构造函数      date(int year=1988, int month=1, int day=1);        // 拷贝构造函数      date(const date& d);        // 赋值运算符重载      date& operator=(const date& d);        // 日期+=天数      date& operator+=(int day);        // 日期+天数      date operator+(int day);        // 日期-天数      date operator-(int day);        // 日期-=天数      date& operator-=(int day);        // 前置++      date& operator++();        // 后置++      date& operator++(int);        // 后置--      date& operator--(int);        // 前置--      date& operator--();        // >运算符重载      bool operator>(const date& d);        // ==运算符重载      bool operator==(const date& d);        // >=运算符重载      bool operator>=(const date& d);        // <运算符重载      bool operator<(const date& d);        // <=运算符重载      bool operator<=(const date& d);        // !=运算符重载      bool operator!=(const date& d);        // 日期-日期 返回两个日期之间相隔的具体天数      int operator-(const date& d);        //日期展示      void print()      {          cout << _year << " " << _month << " " << _day << endl;        }  private:      int _year;      int _month;      int _day;  };  

    二、具体接口函数实现

    注意:

    因为对于定义在类里面的函数会自动设成内联函数,而只有一些简单的函数才建议设成内联函数,所以实现函数时我们是声明和定义分离(在类里面声明,类外定义)

    在类外实现函数接口需要加上类域名称

    1、获取月份天数

    注意:

    闰年二月与平年二月的天数不同

    实现代码:

      //获取月份天数  int date::getmonthday(int year, int month)  {  	//设置平年月天数数组  	static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//设置成静态避免重复创建  	int day = monthdays[month];  	//对闰年二月的处理  	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))  	{  		day = 29;  	}  	return day;  }    

    2、date打印

    注:打印函数比较简单,设成内联函数很适合,可以直接在类里定义

    实现代码:

      void date::print()  {  	cout << _year << "年" << _month << "月" << _day << "日" << endl;  }  

    3、date构造函数

    注意:

    对于构造函数建议写成全缺省函数(便于无参数初始化),但是只能定义和声明其中一个写缺省

    考虑初始化的日期是否合理

    实现代码:

      //构造函数  //类里声明  date(int year = 0, int month = 1, int day = 1);  //定义  date::date(int year, int month, int day)  {  	// 检查日期的合法性  	if (year >= 0  		&& month > 0 && month < 13  		&& day > 0 && day <= getmonthday(year, month))  	{  		_year = year;  		_month = month;  		_day = day;  	}  	else  	{  		// 严格来说抛异常更好  		cout << "非法日期" << endl;  		cout << year << "年" << month << "月" << day << "日" << endl;  		exit(-1);  	}  }  

    4、date析构函数

    注:对于像date一样的类来说,析构函数(没有需要清理的空间资源),拷贝函数和赋值重载函数(能够完成成员变量浅拷贝)都不用自己写,编译器默认生成的已经足够使用

    实现代码:

      //析构函数  date::~date()  {  	_year = 1;  	_month = 0;  	_day = 0;  }  

    5、date拷贝构造函数

    实现代码:

      //拷贝构造  date::date(const date& d)  {  	_year = d._year;  	_month = d._month;  	_day= d._day;  }  

    6、date赋值重载函数

    注意:

    对于赋值操作符来说,是需要能支持连续赋值的操作,这里我们返回date本身来进行接下来的继续赋值

    实现代码:

      //赋值运算符重载  date& date::operator=(const date& d)  {  	_year = d._year;  	_month = d._month;  	_day = d._day;  	return *this;  }  

    效果图:

    C++类和对象实战之Date类的实现方法

    7、date+=天数

    注意:

    1. +=表示会修改date本身的数据
    2. 处理传入负数天数
    3. 处理好天数进位,月份进位

    实现代码:

      //日期+=天数  date& date::operator+=(int day)  {  	if (day < 0)//处理特殊情况  	{  		*this -= -day;//复用date-=天数  	}  	else  	{  		_day += day;  		while (_day > getmonthday(_year, _month))//处理数据合理性  		{  			_day -= getmonthday(_year, _month);  			_month++;  			if (_month > 12)  			{     			_year++;  				_month = 1;  			}  		}  	}  	return *this;//返回引用,即对象本身  }  

    8、date+天数

    注意:

    +天数表示不会修改date本身的数据(使用const修饰,避免修改)

    逻辑与date+=天数基本一致,可以进行复用

    实现代码:

      date date::operator+(int day) const  {  	date tmp = *this;//赋值重载  	tmp += day;//复用+=重载  	return tmp;//返回值(拷贝构造)  }  

    9、date-=天数

    注意:

    1. +=表示会修改date本身的数据
    2. 处理传入负数天数
    3. 考虑日期的借位,月份的借位

    实现代码:

      //日期-=天数  date& date::operator-=(int day)  {  	if (day < 0)  	{  		*this += -day;//复用date+=天数  	}  	else  	{  		_day -= day;  		while (_day <= 0)//处理数据合理性  		{  			_month--;  			if (_month <= 0)  			{  				_year--;  				_month = 12;  			}  			_day += getmonthday(_year, _month);  		}  	}  	return *this;  }  

    10、date-天数

    注意:

    1. -天数不会修改date本身的数据(使用const修饰,避免修改)
    2. 逻辑与date-=天数基本一致,可以进行复用

    实现代码:

      date date::operator-(int day) const  {  	date tmp = *this;  	tmp -= day;  	return tmp;  }  

    11、++date

    注意:

    前置++表示,date先增后使用

    实现代码:

      //++date  date& date::operator++()  {  	*this += 1;//复用date+=天数  	return *this;  }  

    12、date++

    注意:

    语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载

    后置++表示先使用后自增

    实现代码:

      //date++  date date::operator++(int)  {  	date tmp = *this;//保存一份日期  	*this += 1;//自增当前日期  	return tmp;//返回自增前的日期  }  

    13、–date

    实现代码:

      //--date  date& date::operator--()  {  	*this -= 1;  	return *this;  }  

    14、date–

    实现代码:

      //date--  date date::operator--(int)  {  	date tmp = *this;  	*this -= 1;  	return tmp;  }  

    15、日期比较

    注:可以多次复用

    实现代码:

      //日期比较  bool date::operator>(const date& d) const  {  	if (_year > d._year)  	{  		return true;  	}  	else if(_year == d._year)  	{  		if (_month > d._month)  		{  			return true;  		}  		else if(_month == d._month)  		{  			if (_day > d._day)  			{  				return true;  			}  	}  	}> 	return false;  }    bool date::operator==(const date& d) const  {  	return _year == d._year && _month == d._month && _day == d._day;  }    bool date::operator<(const date& d) const  {  	return !(*this >= d);  }    bool date::operator>=(const date& d) const  {  	return *this > d || *this == d;  }    bool date::operator<=(const date& d) const  {  	return !(*this > d);  }    bool date::operator!=(const date& d) const  {  	return !(*this == d);  }  

    16、date相减

    实现代码:

       //日期减日期   int date::operator-(const date& d) const   {   	//确定日期的大小   	date max = *this;   	date min = d;   	if (*this < d)//复用日期比较   	{   		max = d;   		min = *this;   	}   	int day = 0;   	while (max != min)   	{   		++min;   		++day;   	}   	return day;   }  

    17、日期输入日期输出

    注意:

    1. 对于输入操作符,我们习惯是cin>>date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)
    2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

    实现代码:

      //输出操作符重载  ostream& operator<<(ostream& _cout, const date& d)  {  	_cout << d._year << "年" << d._month << "月" << d._day << "日" ;  	return _cout;  }  //输出操作符重载  istream& operator>>(istream& _cin, date& d)  > {  	_cin >> d._year >> d._month >> d._day;  	return _cin;  }  

    效果图:

    1. date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)
    2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

    实现代码:

      //输出操作符重载  ostream& operator<<(ostream& _cout, const date& d)  {  	_cout << d._year << "年" << d._month << "月" << d._day << "日" ;  	return _cout;  }  //输出操作符重载  istream& operator>>(istream& _cin, date& d)  {  	_cin >> d._year >> d._month >> d._day;  	return _cin;  }  

    效果图:

    C++类和对象实战之Date类的实现方法

    总结

    到此这篇关于c++类和对象实战之date类实现的文章就介绍到这了,更多相关c++ date类的实现内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

    需要了解更多c/c++开发分享C++类和对象实战之Date类的实现方法,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

    本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

    ctvol管理联系方式QQ:251552304

    本文章地址:https://www.ctvol.com/c-cdevelopment/969616.html

    (0)
    上一篇 2021年12月13日
    下一篇 2021年12月13日

    精彩推荐