(个人练习)日期类实现(c++)

简介: 学习类和对象时候练习的一个日期类,适合初学者,供大家参考。

学习类和对象时候练习的一个日期类,适合初学者,供大家参考。

Date.h

#pragma once
#include<iostream>
#include<assert.h>
class Date
{
  //友元函数  -- 这个函数内部可以使用Date对象私有保护的成员
  friend std::ostream& operator<<(std::ostream& out, const Date& d);
  friend std::istream& operator>>(std::istream& in, Date& d);
public:
  // 频繁调用写成内联
  int GetMonthDay(int year, int month)
  {
    //静态频繁调用
    static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 &&
      ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))))
    {
      return 29;
    }
    else
    {
      return days[month];
    }
  }
  bool CheckDate(Date & d)
  {
    return d._year >= 1 &&
      d._month >= 1 && d._month <= 12
      && d._day >= 1 && d._day <= GetMonthDay(_year, _month);
  }
  //全缺省构造函数
  Date(int year = 1970, int month = 1, int day = 1);
  //拷贝构造
  Date(const Date& d);
  //赋值运算符重载
  Date& operator=(const Date& d);
  //析构函数
  ~Date();
  //取地址运算符重载
  Date* operator&()
  {
    return this;
  }
  //通过这样设置可以保证,类外面的成员只能访问地址,不能修改地址。
  const Date* operator&()const
  {
    return this;
  }
  //运算符重载
  //日期+=天数
  Date& operator+=(int day);
  //日期+天数
  Date operator+(int day) const;
  //日期-天数
  Date operator-(int day) const;
  //日期-天数
  Date& operator-=(int day);
  // 前置++
  Date& operator++();
  //后置 ++
  Date operator++(int);
  // 前置--
  Date& operator--();
  //后置 --
  Date operator--(int);
  bool operator>(const Date& d) const;
  bool operator==(const Date& d) const;
  bool operator>=(const Date& d) const;
  bool operator<(const Date& d) const;
  bool operator<=(const Date& d) const;
  bool operator!=(const Date& d) const;
  void Print() const;
  //void operator<<(std::ostream& out);
  //日期减日期
  int operator-(const Date& d) const;
private:
  int _year;
  int _month;
  int _day;
};
// 因为会频繁的调用,所以搞成内联函数
//又因为内联函数声明和定义最好不要分离,所以写一块
//inline std::ostream& operator<<(std::ostream& out, const Date& d);
//<<运算符重载
// 第二个参数是传this 用的
inline std::ostream& operator<<(std::ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << std::endl;
  return out;
}
//流提取
inline std::istream& operator>>(std::istream& in, Date& d)
{
  in >> d._year >> d._month >> d._day;
  assert(d.CheckDate(d));
  return in;
}

Date.cpp

#include<iostream>
#include<assert.h>
#include"Date.h"
using namespace std;
// 构造
//本身日期就有问题
Date::Date(int year, int month, int day)
{
  _year = year;
  _month = month;
  _day = day;
  //日期非法
  assert(CheckDate(*this));
}
// 拷贝构造
Date::Date(const Date& d)
{
  _year = d._year;
  _month = d._month;
  _day = d._day;
  cout << "Date" << endl;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
  _year = d._year;
  _month = d._month;
  _day = d._day;
  return *this;
}
//析构函数
Date::~Date()
{
  cout << "~Date" << endl;
}
//运算符重载
//日期+=天数
Date& Date::operator+=(const int day)
{
  if (day < 0)
  {
    return *this -= -day;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month > 12)
    {
      _year++;
      _month -= 12;
    }
  }
  return *this;
}
//日期+天数
Date Date::operator+(const int day) const
{
  Date d(*this);
  return d += day;
}
//日期-=天数
Date& Date::operator-=(const int day)
{
  if (day < 0)
  {
    return *this += -day;
  }
  _day -= day;
  while (_day <= 0)
  {
    //注意1月-1,变成0月
    if (1 == _month)
    {
      _month = 12;
      _year--;
    }
    else
    {
      _month--;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}
//日期-天数
Date Date::operator-(const int day) const
{
  Date d(*this);
  return d -= day;
}
// 前置++
Date& Date::operator++()
{
  *this += 1;
  return *this;
}
//后置 ++
Date Date::operator++(int)
{
  //先用再加
  Date tem(*this);
  *this += 1;
  return tem;
}
//前置--
Date& Date::operator--()
{
  return *this -= 1;
}
//后置--
Date Date::operator--(int)
{
  Date tem(*this);
  *this -= 1;
  return tem;
}
bool Date::operator>(const Date& d) const
{
  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;
  }
  else 
  {
    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 
    || *this == d;
}
bool Date::operator<(const Date& d) const 
{
  return !(*this >= d);
}
bool Date::operator<=(const Date& d) const
{
  return !(*this > d);
}
bool Date::operator!=(const Date& d) const 
{
  return !(*this == d);
}
//打印日期
void Date::Print() const
{
  cout << _year << "-" << _month << "-" << _day << endl;
}
//日期减日期
int Date::operator-(const Date& d) const
{
  Date Max(*this);
  Date Min(d);
  int flag = 1;
  if (*this < d)
  {
    Max = d;
    Min = *this;
    flag = -1;
  }
  int day = 0;
  while (Min != Max)
  {
    ++Min;
    ++day;
  }
  return day * flag;
}

因为代码逻辑并不复杂,这里就不再赘述逻辑,请大家谅解。

目录
打赏
0
0
0
0
1
分享
相关文章
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
【C++篇】深度解析类与对象(中)
在上一篇博客中,我们学习了C++类与对象的基础内容。这一次,我们将深入探讨C++类的关键特性,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载、以及取地址运算符的重载。这些内容是理解面向对象编程的关键,也帮助我们更好地掌握C++内存管理的细节和编码的高级技巧。
【C++篇】深度解析类与对象(上)
在C++中,类和对象是面向对象编程的基础组成部分。通过类,程序员可以对现实世界的实体进行模拟和抽象。类的基本概念包括成员变量、成员函数、访问控制等。本篇博客将介绍C++类与对象的基础知识,为后续学习打下良好的基础。
|
1月前
|
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
70 19
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
53 13
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
56 5
【C++面向对象——群体类和群体数据的组织】实现含排序功能的数组类(头歌实践教学平台习题)【合集】
1. **相关排序和查找算法的原理**:介绍直接插入排序、直接选择排序、冒泡排序和顺序查找的基本原理及其实现代码。 2. **C++ 类与成员函数的定义**:讲解如何定义`Array`类,包括类的声明和实现,以及成员函数的定义与调用。 3. **数组作为类的成员变量的处理**:探讨内存管理和正确访问数组元素的方法,确保在类中正确使用动态分配的数组。 4. **函数参数传递与返回值处理**:解释排序和查找函数的参数传递方式及返回值处理,确保函数功能正确实现。 通过掌握这些知识,可以顺利地将排序和查找算法封装到`Array`类中,并进行测试验证。编程要求是在右侧编辑器补充代码以实现三种排序算法
43 5
【C++面向对象——类的多态性与虚函数】计算图像面积(头歌实践教学平台习题)【合集】
本任务要求设计一个矩形类、圆形类和图形基类,计算并输出相应图形面积。相关知识点包括纯虚函数和抽象类的使用。 **目录:** - 任务描述 - 相关知识 - 纯虚函数 - 特点 - 使用场景 - 作用 - 注意事项 - 相关概念对比 - 抽象类的使用 - 定义与概念 - 使用场景 - 编程要求 - 测试说明 - 通关代码 - 测试结果 **任务概述:** 1. **图形基类(Shape)**:包含纯虚函数 `void PrintArea()`。 2. **矩形类(Rectangle)**:继承 Shape 类,重写 `Print
50 4
【C++面向对象——类的多态性与虚函数】编写教学游戏:认识动物(头歌实践教学平台习题)【合集】
本项目旨在通过C++编程实现一个教学游戏,帮助小朋友认识动物。程序设计了一个动物园场景,包含Dog、Bird和Frog三种动物。每个动物都有move和shout行为,用于展示其特征。游戏随机挑选10个动物,前5个供学习,后5个用于测试。使用虚函数和多态实现不同动物的行为,确保代码灵活扩展。此外,通过typeid获取对象类型,并利用strstr辅助判断类型。相关头文件如&lt;string&gt;、&lt;cstdlib&gt;等确保程序正常运行。最终,根据小朋友的回答计算得分,提供互动学习体验。 - **任务描述**:编写教学游戏,随机挑选10个动物进行展示与测试。 - **类设计**:基类
36 3
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等