【C++】踏上C++学习之旅(九):深入“类和对象“世界,掌握编程的黄金法则(四)(包含四大默认成员函数的练习以及const对象)

06-02 1305阅读

文章目录

  • 前言
  • 1. 实现Date类的构造函数
  • 2. 实现Date类的拷贝构造函数
  • 3. 实现Date类的赋值运算符重载
  • 4. 实现各Date对象之间的比较接口
  • 5. 实现Date对象的加减接口
  • 6. const成员
  • 7. 取地址及const取地址操作符重载

    前言

    在我们前面学习到了"类和对象"的四大默认成员函数(构造函数、析构函数、拷贝构造函数、赋值运算符重载),这四大默认成员函数也是我们在以后使用"类和对象"这块知识时经常遇到的。本章将会围绕着如何实现一个Date类,来让大家尽快学会编写和更加深刻理解关于"类"封装的思想在实际当中的应用!

    本文会分板块逐一讲解,在文章的末尾放有本次实现Date类的全部源码。

    【C++】踏上C++学习之旅(九):深入“类和对象“世界,掌握编程的黄金法则(四)(包含四大默认成员函数的练习以及const对象)

    1. 实现Date类的构造函数

    所谓的 “Date” 翻译过来就是 “日期” 的意思,那它的成员变量一定是年月日。那我们就可以这么实现Date类的构造函数。

    class Date
    {
    public:
    	//全缺省的默认构造函数
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    private:
    	int _year; //年
    	int _month; //月
    	int _day; //日
    };
    

    这里我们写成全缺省的默认构造函数是十分有讲究的,一方面当我们先使用这个缺省值时,我们就直接有类实例化出对象就行,不需要显式的传递参数;另一方面,当我们想用自己的传递的参数,就直接显式传递函数即可。一举两得。

    2. 实现Date类的拷贝构造函数

    class Date
    {
    public:
    	//全缺省的默认构造函数
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	//拷贝构造函数
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    

    这里对于拷贝构造函数的思路没有太多的讲解,只需要大家注意一个点就是,形参一定是对于类类型的引用,避免引发无穷的递归调用。

    3. 实现Date类的赋值运算符重载

    class Date
    {
    public:
    	//全缺省的默认构造函数
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	//拷贝构造函数
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	//赋值运算符重载
    	Date& operator=(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    		return *this;
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    

    关于赋值运算符重载的返回值,一定是*this,而不是this。因为我们是要返回这个待赋值对象的引用,而this是这个待赋值对象的指针类型为Date* const。还有的人会考虑生命周期的问题,其实这里大可不必担心,虽然this指针的生命周期在这个成员函数中,出了这个作用域就会被销毁,但是我们返回的不是this而是*this,*this就是那个待拷贝的对象,所以其的生命周期是在main函数中的!

    4. 实现各Date对象之间的比较接口

    本次的是实现主要涉及到大小之间的比较,目的是锻炼大家对于运算符重载的用法。

    //年份之间的比较大小
    class Date
    {
    public:
    	//全缺省的默认构造函数
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	//拷贝构造函数
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	//年份之间的比较大小
    	bool operator>(const Date& d)
    	{
    		if (_year > d._year)
    		{
    			return true;
    		}
    		else if (_year == d._year && _month > d._month)
    		{
    			return true;
    		}
    		else if (_year == d._year && _month == d._month && _day > d._day)
    		{
    			return true;
    		}
    		return false;
    	}
    	bool operator==(const Date& d)
    	{
    		return _year == d._year && _month == d._month && _day == d._day;
    	}
    	bool operator!=(const Date& d)
    	{
    		return !(*this == d);
    	}
    	bool operator>=(const Date& d)
    	{
    		return (*this == d) || (*this > d);
    	}
    	bool operator
    		return (*this == d) || (*this  12)
    			{
    				tmp._year++;
    				tmp._month = 1;
    			}
    		}
    		return tmp;
    	}
    	Date& operator+=(int day)
    	{
    		_day += day;
    		while (_day > GetMonthDay())
    		{
    			_day -= GetMonthDay();
    			++_month;
    			if (_month > 12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return *this;
    	}
    	Date& operator-=(int day)
    	{
    		_day -= day;
    		while (_day 
    			--_month;
    			if (_month  12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return *this;
    	}
    	Date operator++(int)
    	{
    		Date tmp = *this;
    		_day += 1;
    		while (_day > GetMonthDay())
    		{
    			_day -= GetMonthDay();
    			++_month;
    			if (_month > 12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return tmp;
    	}
    private:
    	int GetMonthDay()
    	{
    		int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    		if (_month == 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
    		{
    			return 29;
    		}
    		return month_day[_month];
    	}
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    

    这个可以很好的锻炼大家对于前置++和后置++的写法。

    到这里Date类我们就全部实现完毕了,是不是很简单呢。一下就是Date类实现全部的源码:

    class Date
    {
    public:
    	//全缺省的默认构造函数
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	//拷贝构造函数
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	Date operator+(int day) const
    	{
    		Date tmp = *this;
    		tmp._day += day;
    		//我们还要考虑一下天数相加会导致的 月进位 甚至是 年进位
    		//所以我们得获取每个月的天数
    		while (tmp._day > tmp.GetMonthDay())
    		{
    			tmp._day -= tmp.GetMonthDay();
    			tmp._month++;
    			if (tmp._month > 12)
    			{
    				tmp._year++;
    				tmp._month = 1;
    			}
    		}
    		return tmp;
    	}
    	//年份之间的比较大小
    	bool operator>(const Date& d)
    	{
    		if (_year > d._year)
    		{
    			return true;
    		}
    		else if (_year == d._year && _month > d._month)
    		{
    			return true;
    		}
    		else if (_year == d._year && _month == d._month && _day > d._day)
    		{
    			return true;
    		}
    		return false;
    	}
    	bool operator==(const Date& d)
    	{
    		return _year == d._year && _month == d._month && _day == d._day;
    	}
    	bool operator!=(const Date& d)
    	{
    		return !(*this == d);
    	}
    	bool operator>=(const Date& d)
    	{
    		return (*this == d) || (*this > d);
    	}
    	bool operator
    		return (*this == d) || (*this  12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return *this;
    	}
    	Date& operator++()
    	{
    		_day += 1;
    		while (_day > GetMonthDay())
    		{
    			_day -= GetMonthDay();
    			++_month;
    			if (_month > 12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return *this;
    	}
    	Date operator++(int)
    	{
    		Date tmp = *this;
    		_day += 1;
    		while (_day > GetMonthDay())
    		{
    			_day -= GetMonthDay();
    			++_month;
    			if (_month > 12)
    			{
    				++_year;
    				_month = 1;
    			}
    		}
    		return tmp;
    	}
    	Date& operator-=(int day)
    	{
    		_day -= day;
    		while (_day 
    			--_month;
    			if (_month 
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码