【C++初阶】4. Date类的实现

简介: 【C++初阶】4. Date类的实现

如果下面博客有不理解的地方,可以查看源码:代码提交:日期类的实现

1. 构造函数的实现

由于系统实现的默认构造函数即便采用默认值的形式也只能存在1个固定的默认日期(例如:1997-1-1)。所以,构造函数需要显示实现

//判断日期是否正确
bool Date::IsTrueDate(int year, int month, int day)
{
   
   
    static int arr[13] = {
   
    0,31,29,31,30,31,30,31,31,30,31,30,31 };
    if (month > 12)
        return false;
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) && month == 2)
    {
   
   
        if (day > 28)
        {
   
   
            return false;
        }
    }
    if (day > arr[month])
        return false;
    return true;
}
class Date
{
   
   
public:
    // 声明定义分离
    bool IsTrueDate(int year, int month, int day);
    Date(int year = 2023,int month = 3,int day = 18)
    {
   
   
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};

默认构造函数有三类:无参,全缺省参数,系统默认生成
这边采用的是全缺省的方式:要根据传递过来的参数来实现不同的日期
==声明与定义是否需要分离?==
对于频繁调用的接口(例如:构造函数),推荐声明与定义不分离,编译器将其转化为内联函数(inline),在调用的地方直接展开(减少拷贝,提高效率)
对于不频繁调用且冗余的接口,推荐分离,因为不方便展开(展开会显得代码太长)

2. 析构函数

由于没有资源的申请,所以不需要手动实现析构函数,采用系统默认的即可

3. 拷贝构造函数

因为编译器默认生成的拷贝为浅拷贝,可以满足需求,所以可以不需要实现 但是日期类的

    //隐藏的this指针 Date(Date* const this,const Date& d) 
    //Date d2(d1)
    Date(const Date& d)
    {
   
   
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

4. 赋值重载

因为编译器默认生成的拷贝为浅拷贝,可以满足需求,所以可以不需要实现

5. Print

    void Print()
    {
   
   
        cout << _year << "-" << _month << "-" << _day << endl;
    }

TestDate01

在这里插入图片描述

6. 重载 == (operator==)

    bool operator==(const Date& d) const
    {
   
   
        return _year == d._year
            && _month == d._month
            && _day == d._day;
    }

==为啥末尾要加const修饰呢?==
末尾加上const修饰其实修饰的是隐藏的this指针,因为==的运算符重载不会改变this指针所指向的内容。
加上const修饰之后,可以保护数据不被修改,增加代码的健壮性。

7. 重载 > (operator>)

声明与定义分离

    bool operator>(const Date& d) const;
bool Date::operator>(const Date& d) const
{
   
   
    if (_year > d._year)
        return true;
    if (_year == d._year && _month > d._month)
        return true;
    if (_year == d._year && _month == d._month && _day > d._day)
        return true;
    return false;
}

8. 重载 >= (operator>=)

    bool operator>=(const Date& d) const;
bool Date::operator>=(const Date& d) const
{
   
   
    // 接口的复用 operator== 和 operator>
    return (*this == d) || (*this > d);
}

9. 重载 != (operator!=)

    bool operator!=(const Date& d) const;
bool Date::operator!=(const Date& d) const
{
   
   
    return !(*this == d);
}

10. 重载 < (operator<)

    bool operator<(const Date& d) const;
bool Date::operator<(const Date& d) const
{
   
   
    return !(*this >= d);
}

11. 重载 <= (operator<)

    bool operator<=(const Date& d) const;
bool Date::operator<=(const Date& d) const
{
   
   
    return (*this < d) && (*this == d);
}

TestDate02

在这里插入图片描述

12. 重载 += (operator+=)

//在Date.cpp中定义
int GetMonthDay(int year, int month)
{
   
   
    static int monthDayArray[13] = {
   
    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;
    }
    else
    {
   
   
        return monthDayArray[month];
    }
}
Date& Date::operator+=(int day)
{
   
   
    //加个判断
    if (day < 0)
    {
   
   
        // 接口复用 operator-= 
        // 因为天数小于0,所以带负号
        *this -= -day;
    }
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
   
   
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month > 12)
        {
   
   
            _year++;
            _month = 1;
        }
    }
    return *this;
}

13. 重载 -= (operator-=)

    Date& operator-=(int day);
Date& Date::operator-=(int day)
{
   
   
    //加个判断
    if (day < 0)
    {
   
   
        // 接口复用 operator+= 
        // 因为天数小于0,所以带负号
        *this += -day;
    }
    _day -= day;
    while (_day < 0)
    {
   
   
        _day += GetMonthDay(_year, _month-1);
        _month--;
        if (_month < 1)
        {
   
   
            _year--;
            _month = 13;
        }
    }
    return *this;
}

14. 重载 - (operator-)

    Date operator-(int day);
Date Date::operator-(int day)
{
   
       
    //拷贝构造 ret对象
    Date ret = *this;
    //重载-=
    ret -= day;
    return ret;
}

==为啥这边operator-的返回值是Date(传值返回) 而不是传引用返回呢?==
因为-操作并不会影响this指针指向对象的值,所以需要拷贝构造一个临时变量进行操作符运算

15. 重载 + (operator+)

Date Date::operator+(int day)
{
   
   
    //拷贝构造 ret对象
    Date ret = *this;
    //重载+=
    ret += day;
    return ret;
}

TestDate03

在这里插入图片描述

16. 重载 后置++ (operator++)(int)

    // 后置++
    Date operator++(int);
// 后置++
Date Date::operator++(int)
{
   
   
    Date ret = *this;
    *this += 1;
    return ret;
}

为啥后置++要传值返回呢?前置++可以传引用返回呢
因为后置++是先返回值,再进行++操作。
而前置++是先++,再返回++后的值。

17. 重载 前置++(operator++)

    // 前置++
    Date& operator++();
// 前置++
Date& Date::operator++()
{
   
   
    *this += 1;
    return *this;
}

18. 重载 后置-- (operator--)(int)

    // 后置--
    Date operator--(int);
// 后置--
Date Date::operator--(int)
{
   
   
    Date ret = *this;
    *this -= 1;
    return ret;
}

19. 重载 前置-- (operator--)

    // 前置--
    Date& operator--();
// 前置--
Date& Date::operator--()
{
   
   
    *this -= 1;
    return *this;
}

TestDate04

在这里插入图片描述

20. 重载- (operator-)

    // 日期减
    int operator-(const Date& d);
int Date::operator-(const Date& d)
{
   
   
    //在二者中找一个小的日期类不断++ 直到与大的日期相等即可
    int date = 0;
    // 假设this大 d小
    Date max = *this;
    Date min = d;
    if (max < min)
    {
   
   
        // this小 d大 
        max = d;
        min = *this;
    }
    while (min != max)
    {
   
   
        ++date;
        ++min;
    }
    return date;
}

TestDate05

在这里插入图片描述

21. 流插入重载 (operator<<)

因为流插入,流提取的操作符是cout<<d1 cin >>d1,是将类型流向控制台或者从控制台提取出来,所以不能实现成成员函数,否则就是d1.operator<<(d1<<cout) 不符合实际 所以将其实现成全局函数
在这里插入图片描述
在类中任意位置声明友元函数即可。

class Date
{
   
   
    // 友元声明(类的任意位置)
    friend ostream& operator<<(ostream& out, const Date& d);
    friend istream& operator>>(istream& out, Date& d);
    //....

==这里的operator>>参数为啥是Date& 而不是const Date& 呢?==
因为流插入就是要修改Date对象的,所以不能拿const修饰

inline ostream& operator<<(ostream& out, const Date& d)
{
   
   
    out << d._year << "-" << d._month << "-" << d._day << endl;
    return out;
}

==这里能不能不设计成inline内联呢?==
答案是不行的,因为该函数是定义在Date.h文件当中,而Date.h又会在两个文件中都引用,在编译阶段,Date.cpp 和 Test.cpp 两个文件当中的头文件分别展开 那么在链接的过程中,不加inline就会使函数出现在符号表当中,而两份代码会导致两个地址,出现链接错误。
所以必须要inline内联,或者定义成static静态函数,改变其链接属性,使其无法出现在符号表

==这里函数要返回值是为啥?ostream&==
是因为要连续赋值,cin >>d1>>d2;类似这种场景,如果不传返回值,则无法进行连续操作

22. 流提取重载 (operator>>)

inline istream& operator>>(istream& in, Date& d)
{
   
   
    in >> d._year >> d._month >> d._day;
    return in;
}

TestDate06

在这里插入图片描述
那么类的基本实现就完成啦\(^o^)/~ ,欢迎各位大佬指正,一起加油,共勉!!!

相关文章
|
3天前
|
设计模式 安全 编译器
【C++11】特殊类设计
【C++11】特殊类设计
22 10
|
8天前
|
C++
C++友元函数和友元类的使用
C++中的友元(friend)是一种机制,允许类或函数访问其他类的私有成员,以实现数据共享或特殊功能。友元分为两类:类友元和函数友元。类友元允许一个类访问另一个类的私有数据,而函数友元是非成员函数,可以直接访问类的私有成员。虽然提供了便利,但友元破坏了封装性,应谨慎使用。
39 9
|
3天前
|
存储 编译器 C语言
【C++基础 】类和对象(上)
【C++基础 】类和对象(上)
|
12天前
|
编译器 C++
【C++】string类的使用④(字符串操作String operations )
这篇博客探讨了C++ STL中`std::string`的几个关键操作,如`c_str()`和`data()`,它们分别返回指向字符串的const char*指针,前者保证以&#39;\0&#39;结尾,后者不保证。`get_allocator()`返回内存分配器,通常不直接使用。`copy()`函数用于将字符串部分复制到字符数组,不添加&#39;\0&#39;。`find()`和`rfind()`用于向前和向后搜索子串或字符。`npos`是string类中的一个常量,表示找不到匹配项时的返回值。博客通过实例展示了这些函数的用法。
|
12天前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `&lt;`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。
|
12天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。
|
12天前
|
C++
C++】string类的使用③(修改器Modifiers)
这篇博客探讨了C++ STL中`string`类的修改器和非成员函数重载。文章介绍了`operator+=`用于在字符串末尾追加内容,并展示了不同重载形式。`append`函数提供了更多追加选项,包括子串、字符数组、单个字符等。`push_back`和`pop_back`分别用于在末尾添加和移除一个字符。`assign`用于替换字符串内容,而`insert`允许在任意位置插入字符串或字符。最后,`erase`函数用于删除字符串中的部分内容。每个函数都配以代码示例和说明。
|
12天前
|
安全 编译器 C++
【C++】string类的使用②(元素获取Element access)
```markdown 探索C++ `string`方法:`clear()`保持容量不变使字符串变空;`empty()`检查长度是否为0;C++11的`shrink_to_fit()`尝试减少容量。`operator[]`和`at()`安全访问元素,越界时`at()`抛异常。`back()`和`front()`分别访问首尾元素。了解这些,轻松操作字符串!💡 ```
|
12天前
|
存储 编译器 Linux
【C++】string类的使用②(容量接口Capacity )
这篇博客探讨了C++ STL中string的容量接口和元素访问方法。`size()`和`length()`函数等价,返回字符串的长度;`capacity()`提供已分配的字节数,可能大于长度;`max_size()`给出理论最大长度;`reserve()`预分配空间,不改变内容;`resize()`改变字符串长度,可指定填充字符。这些接口用于优化内存管理和适应字符串操作需求。
|
12天前
|
C++ 容器
【C++】string类的使用①(迭代器接口begin,end,rbegin和rend)
迭代器接口是获取容器元素指针的成员函数。`begin()`返回首元素的正向迭代器,`end()`返回末元素之后的位置。`rbegin()`和`rend()`提供反向迭代器,分别指向尾元素和首元素之前。C++11增加了const版本以供只读访问。示例代码展示了如何使用这些迭代器遍历字符串。