欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 【C++课程学习】:Data类的实现

【C++课程学习】:Data类的实现

2025/2/23 6:19:40 来源:https://blog.csdn.net/djdjiejsn/article/details/139607320  浏览:    关键词:【C++课程学习】:Data类的实现

🎁个人主页:我们的五年

🔍系列专栏:C++课程学习

🎉欢迎大家点赞👍评论📝收藏⭐文章

目录

 🍩1.头文件

🍩2.实现文件:

 🍩3.分析:

🍟3.1Data的构造函数和析构函数:

🍟3.2拷贝构造:


 前言:
类学的差不多的,我们就日期类来对前面类的学习做一个巩固,然后学习一下运算符重载。

 🍩1.头文件

#pragma once
#include<iostream>
#include<stdbool.h>
#include<assert.h>
#include<math.h>
using namespace std;class Data {friend ostream& operator<<(ostream& out, Data& d);friend istream& operator>>(istream& in, Data& d);
public://全缺省的构造函数Data(int year = 1, int month = 1, int day = 1) {_year = year;_month = month;_day = day;}//拷贝构造函数Data(const Data& d){_year = d._year;_month = d._month;_day = d._day;}//运算符重载bool operator==(const Data& d);bool operator<(const Data& d);bool operator>(const Data& d);bool operator<=(const Data& d);bool operator>=(const Data& d);bool operator!=(const Data& d);Data& operator = (const Data& d);//日期+天数Data& operator+=(int day);Data operator+(int day);//日期-天数Data& operator-=(int day);Data operator-(int day);//日期的前置和后置++Data& operator++();Data operator++(int);//日期的前置和后置--Data& operator--();Data operator--(int);//日期减日期=相差的天数int operator-(const Data& d);//void operator<<(ostream& out);//内联函数,获取year年第month的天数int GetDay(int year, int month) {assert(month > 0 && month < 13);static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//闰年的二月返回29if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))return 29;return days[month];}void print() {cout << _year << "-" << _month << "-" << _day << endl;}int Getyear() {return _year;}int Getmonth() {return _month;}int Getday() {return _day;}//析构函数~Data() {_year = -1;_month = -1;_day = -1;}
private:int _year;int _month;int _day;
};

🍩2.实现文件:

#include"Data.h"
bool Data::operator==(const Data& d) {return _year == d._year&& _month == d._month&& _day == d._day;
}bool Data::operator<(const Data& d) {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 Data::operator<=(const Data& d) {return (*this == d) || (*this < d);
}bool Data::operator>(const Data& d) {return !(*this <= d);
}bool Data::operator>=(const Data& d) {return (*this == d) || (*this) > d;
}
bool Data::operator!=(const Data& d) {return !(*this == d);
}Data& Data::operator = (const Data& d) {_year = d._year;_month = d._month;_day = d._day;return *this;
}Data& Data::operator+=(int day) {if (day < 0){return *this -= -day;}_day += day;while (_day > GetDay(_year,_month)){_day -= GetDay(_year, _month);_month++;if (_month == 13){_month = 1;_year++;}}return *this;
}Data Data::operator+(int day) {Data a = *this;a += day;return a;
}
Data& Data::operator-=(int day) {if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetDay(_year, _month);}return *this;
}Data Data::operator-(int day) {Data a = *this;a -= day;return a;
}
Data& Data::operator++() {return *this += 1;
}Data Data::operator++(int) {Data a = *this;*this += 1;return a;
}Data& Data::operator--() {return *this -= 1;
}Data Data::operator--(int) {Data tmp = *this;*this -= 1;return tmp;
}int Data::operator-(const Data& d) {int day = 0;Data max = *this;Data min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}while (min != max){++min;++day;}return day*flag;
}ostream& operator<<(ostream& out,Data& d){out << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Data& d) {in >> d._year >> d._month >> d._day;return in;
}

 🍩3.分析:

日期的基本成员就是年(year),月(month),和日(day)。

    int _year;
    int _month;
    int _day;

🍟3.1Data的构造函数和析构函数:

构造函数我们用了全缺省的构造函数:


构造函数:

如果没有传参数的,我们就初始化为1。其实换成year的缺省值换为1970,moth换成1,day换成1,也是可以的,时间戳从这时候开始计时。

   //全缺省的构造函数

Data(int year = 1, int month = 1, int day = 1) {
        _year = year;
        _month = month;
        _day = day;
    }


析构函数:

没有资源要进行清理,我们把类的属性都变为-1。

//析构函数

~Data() {
        _year = -1;
        _month = -1;
        _day = -1;
    }

🍟3.2拷贝构造:

  加上const修饰一下d,只读。

 //拷贝构造函数
    Data(const Data& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词