【C++初阶学习】C++类和对象实战-Date类的实现(1)

简介: 【C++初阶学习】C++类和对象实战-Date类的实现(1)

零、前言


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


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


1、获取月份天数


  • 注意:


  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构造函数


  • 注意:


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


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


  • 实现代码:


//构造函数
//类里声明
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赋值重载函数


  • 注意:


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


  • 实现代码:


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


效果图:


image.png



相关文章
|
2天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
4天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
4天前
|
C++ Linux
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
18 0
|
4天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
4天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
4天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
4天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
4天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)