Date类实现运算符和赋值重载

简介: Date类实现运算符和赋值重载

通过Date类来实现对于前文学习接触的赋值型重载,构造函数,析构函数,下面分为三个文件

1.test.cpp 进行测试的.cpp文件

2.Date.h Date的头文件,里面是Date类的成员函数声明和成员变量

3.Date.cpp 对于头文件Date.h的处理,定义Date的成员函数

Date.h

头文件,包括Date类的成员变量和成员函数的声明

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Date {
public:
  Date(int year = 1900, int month = 1, int day = 1);
  // 拷贝构造函数
  // d2(d1)
  Date(const Date& d);
  // 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
  Date& operator=(const Date& d);
  // 析构函数
  ~Date();
  // 日期+=天数
  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);
  int getDays(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};

Date.cpp

对于Date.h头文件的成员函数进行定义,实现赋值重载和运算符重载

#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
//Date:: 开放类域
Date::Date(int year, int month , int day) {
  _year = year;
  _month = month;
  _day = day; 
}
// d2(d1)
//拷贝构造函数
Date::Date(const Date& d) {
  if (this != &d) {
    _year = d._year;
    _month = d._month;
    _day = d._day;
  }
}
// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d) {
  (*this)._year = d._year;
  (*this)._month = d._month;
  (*this)._day = d._day;
  return *this;
}
Date::~Date() {
  cout <<(this)<< "调用析构函数" << endl;
}
Date& Date::operator+=(int day) {
  _day += day;
  while (_day > getDays(*this)) {
    _day -= getDays(*this);
    _month++;
    if (_month == 13) {
      _month = 1;
      _year++;
    }
  }
  return *this;
}
//+的话不能改变this的数值,所以需要使用临时变量
Date Date::operator+(int day) {
  Date p = *this;
  p.operator+=(day);
  return p;
}
//日期相减
Date Date::operator-(int day) {
  Date p = *this;
  p._day-= day;
  if (p._day > 0) {
    return p;//如果减去天数之后,day是个整数,说明还在这个月内,只是天数小了,所以可以直接返回
  }
  p._day = abs(p._day);//反之取得绝对值,然后跟上一个月的天数比较,如果大于,那就减去
  while (true) {
    p._month--;
    if (p._month == 0) {
      p._month = 12;
    }
    if (p._day > getDays(p)) {
      p._day -= getDays(p);
    }
    else {
      p._day = getDays(p) - p._day;
      return p;
    }
  }
}
Date& Date::operator-=(int day) {
  *this = (*this).operator-(day);
  return *this;
}
//前置++  括号为无参函数
Date& Date::operator++() {
  (*this).operator+=(1);
  return *this;
}
//后置++,括号中的形参为int类型  没有实际意义,只是为了区分前置和后置++
Date Date::operator++(int) {
  //后置++  需要使用临时变量
  Date date = *this;
  (*this).operator+=(1);
  return date;//返回的是形参,需要进行拷贝赋值返回处理
}
//获得对应月的天数
int Date::getDays(const Date& d) {
  int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if (d._month == 2 && ((d._year % 4 == 0 && d._year % 100 != 0) || d._year % 400 == 0)) {
    return 29;
  }
  else {
    return day[d._month];
  }
}
// 后置--
Date Date::operator--(int) {
  Date date = (*this);
  (*this).operator-=(1);
  return date;
}
//
// 前置--
Date& Date::operator--() {
  (*this).operator-=(1);
  return *this;
}
// >运算符重载
//日期大小比较:如果日期靠后,就大,反之为小
bool Date::operator>(const Date& d) {
  if ((*this)._year > d._year) {
    return true;
  }
  else if(((*this)._year == d._year)&&((*this)._month>d._month)) {
    return true;
  }
  else if (((*this)._year == d._year) && ((*this)._month > d._month) && ((*this)._day > _day)) {
    return true;
  }
  else {
    return false;
  }
}
// ==运算符重载
bool Date::operator==(const Date& d) {
  return _year == d._year && _month == d._month && _day == d._day;
}
//
// >=运算符重载
bool Date::operator >= (const Date& d) {
  return (*this).operator>(d) || (*this).operator==(d);
}
//
// <运算符重载
bool Date::operator < (const Date& d) {
  if ((*this).operator>=(d)) {
    return false;
  }
  else {
    return true;
  }
}
 <=运算符重载
bool Date::operator <= (const Date& d) {
  return (*this).operator<(d) || (*this).operator==(d);
}
// !=运算符重载
bool Date::operator != (const Date& d) {
  if ((*this).operator==(d)) {
    return false;
  }
  else {
    return true;
  }
}
// 日期-日期 返回天数
//
bool getyear(int num) {
  if ((num % 4 == 0 && num % 100 != 0) || num % 400 == 0) {
    return true;
  }
  else {
    return false;
  }
}
int Date::operator-(const Date& d) {
  int num = 0;
  while ((*this).operator!=(d))
  {
    (*this).operator++();
    num++;
  }
  return num;
}

test.cpp

里面又main函数,是作为测试用的.cpp文件

#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
void Test()
{
  Date p1(2020,10,10);
  //cout << p.getDays(p) << endl;
  //p.operator+=(1000);
  Date c = p1.operator+(100);//366
  cout << p1.operator-(c) << endl;
}
int main()
{
  Test();
  return 0;
}

这个test.cpp文件里面的Test()函数在进行测试,请自行添加测试

总结

赋值重载的话,返回值有两种

  1. 如果是临时变量,返回值为类类型 Date
  2. 如果不是临时变量,是*this 那就是传引用返回 Date&

运算符重载的话,返回值一般是bool,而且有些是可以利用其他运算符重载搞定的

  1. 比如> 和== 这两个就可以将各种大小比较搞定,是为了提高代码复用率
  2. 且返回值一般为bool,传参是两个参数,一个是this,一个是const Date& p 使用传引用参数是为了提高效率
相关文章
|
5月前
|
C++
33 C++ - 可重载的运算符
33 C++ - 可重载的运算符
22 0
|
1月前
|
C++
C++运算符号重载详解
C++运算符号重载详解
|
2月前
|
存储 C++
C++ 操作重载与类型转换(二)
C++ 操作重载与类型转换(二)
42 2
|
2月前
|
自然语言处理 安全 C++
C++ 操作重载与类型转换(一)
C++ 操作重载与类型转换(一)
50 3
|
5月前
|
编译器 C++
36 C++ - 赋值(=)运算符重载
36 C++ - 赋值(=)运算符重载
20 0
|
11月前
|
存储 编译器 C++
类的默认成员函数、赋值运算符重载(二)
如果一个类中什么成员都没有,简称为空类。 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
42 0
|
11月前
|
编译器 C++
类的默认成员函数、赋值运算符重载(一)
如果一个类中什么成员都没有,简称为空类。 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
69 0
|
11月前
12-赋值运算符重载
12-赋值运算符重载
定义了一个类A,S是类外的一个函数,通过A.S=S进行赋值
假设类 A 已经定义好了,现在可以通过 A.S = S 的方式将函数 S 赋值给类 A。这样做的效果是,将 S 函数作为类 A 的一个属性,并且可以通过该属性来调用函数 S。 下面是一个简单的例子:
【C++】-- 实现Date类的各种运算符重载
【C++】-- 实现Date类的各种运算符重载