C++——类和对象3|日期类型|Cout运算符重载|Cin运算符重载|const成员|(上)

简介: 笔记

日期类型


Date.h 1.png

#include<iostream>
using namespace std;
class Date
{
public:
  // 获取某年某月的天数
  int GetMonthDay(int year, int month)
  {
    static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day = days[month];
    if (month == 2
      && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
      day += 1;
    }
    return day;
  }
  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  void Print();
  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);
  Date operator+(int day);
  Date& operator++();//默认是前置++,返回+完后的值,用引用返回
  Date operator++(int i);//后前置++,里面要写一个参数,这里的参数可以不用形参接收,括号里直接写int就行,但加了参数也无所谓,这里的参数只是为了区分
  //C++为了区分前后置++,使用重载进行区分,后置++重载增加一个int参数跟前置构成函数重载进行区分
  Date& operator+=(int day);
  Date operator-(int day);
  Date& operator-= (int day);
  Date& operator--();
  Date  operator--(int);
  int operator-(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};

Date.cpp

#include"date.h"
bool Date::operator==(const Date& d)
{
  return _year == d._year &&
    _month == d._month &&
    _day == d._day;
}
bool Date::operator!=(const Date& d)
{
  return !(*this == d);
}
bool Date::operator>(const Date& d)
{
  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)
{
  return !(*this >= d);
}
bool Date::operator>=(const Date& d)
{
  return (*this > d) || (*this == d);
}
bool Date::operator<=(const Date& d)
{
  return !(*this > d);
}
// 自身要改变
Date&  Date::operator+=(int day)
{
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month == 13)
    {
      _month = 1;
      _year ++;
    }
  }
  return *this;
}
//自身不改变
Date  Date::operator+(int day)
{
  Date ret(*this);//拷贝构造
  //Date ret= *this;在这里,这条语句是拷贝构造,拿*this初始化ret,已经存在的对线才是赋值
  ret += day;
  return ret;//传值返回的时候,会生成一份拷贝
}
void  Date::Print()
{
  cout << _year<<"/" << _month<<"/"<<_day << endl;
}
Date& Date::operator++()//前置
{
  *this += 1;
  return *this;
}
Date Date::operator++(int)//后置,返回++之前的值
{
  Date tmp(*this);
  *this += 1;
  return tmp;
}
Date& Date::operator-= (int day)
{
  _day -= day;
  while (_day <= 0)
  {
    --_month;
    if (_month == 0)
    {
      --_year;
      _month = 12;
    }
    _day += GetMonthDay(_year,_month);
  }
  return *this;
}
Date Date::operator-(int day)
{
  Date ret = *this;
  ret -= day;
  return ret;
}
Date& Date::operator--()//前置--
{
  *this -= 1;
  return *this;
}
Date   Date::operator--(int)//后置--
{
  Date tmp = *this;
  *this -= 1;
  return tmp;
}
int  Date::operator-(const Date& d)//日期相减
{
  int flag = 1;
  Date max = *this;
  Date min = d;
  if (*this < d)
  {
    min = *this;
    max = d;
    flag = -1;
  }
  int n = 0;
  while (min != max)
  {
    ++min;
    ++n;
  }
  return n*flag;
}

Test.cpp

int main()
{
  Date d1(2001, 1, 3);
  Date d2(2023, 7, 23);
  Date d3(2022, 6, 23);
  cout << (d1 < d2) << endl;
  cout << (d1 > d2) << endl;
  cout << (d1 == d2) << endl;
  cout << (d1 < d3) << endl;
  cout << (d1 > d3) << endl;
  cout << (d1 == d3) << endl;
  Date ret1 = d1 + 100;
  ret1.Print();
  Date ret2=++d1;//转换为d1.operator++(&d1)
  ret2.Print();
  Date ret3=d2++;//转换为d1.operator++(&d2,0)
  ret3.Print();
  d1 += 100;
  d1.Print();
  d1 -= 100;
  d1.Print();
  Date ret4 = d1 - 1000;
  ret4.Print();
  int ret5 = d1 - d2;
  cout << ret5 << endl;
  return 0;
}

2.png

一个类到底可以重载哪些运算符,要看哪些运算符对这个类型有意义

日期减天数思路:先给天减,如果不够(<=0)就借位

程序有缺陷,构造这里出现了问题

3.png

我们在类里面加上判断日期的函数即可或者用assert去判

  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
         //assert(CheckDate());
    if (!CheckDate())
    {
      Print();
      cout << "日期非法" << endl;
    }
  }
  bool CheckDate()
  {
    if (_year >= 1 && _month < 13 && _month>0 && _day > 0 && _day <= GetMonthDay(_year, _month))
      return true;
    else
      return false;
  }

4.png

加一个负数会报错,我们只需在+=里面进行调整就行,因为+是通过+=实现的

Date&  Date::operator+=(int day)
{
  if (day < 0)
  {
    return *this -= -day;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month == 13)
    {
      _month = 1;
      _year ++;
    }
  }
  return *this;
}

5.png6.png7.png

同理对-=也修改,如果天数小于0,-=之后 会因为负负得正,给加几天,我们此时调整一下即可


实现Cout运算符重载



C++ cout和cin能自动识别的原因:

1.库里面写好了运算符重载

2.自动识别类型,它们构成重载函数 8.png

运算符重载:让自定义类型对象可以用运算符,转换成调用这个重载函数

函数重载:支持函数名相同的函数同时存在

虽然俩者都是重载,但它们俩者没有关系

但如果这样使用cout和cin会报错9.png

此时我们自己在类里面写运算重载符就可以解决这个问题


cout是ostream类型对象,cin是istream类型对象


void Date::operator<<(ostream& out)
{
  out << _year << "-" << _month << _day;
}

此时如果写成这个样子,还是会报错10.png

但这样可以

11.png

运算符有多个操作数的时候,第一个参数是左操作数,第二个参数是右操作数12.png

d1是第一个操作数(左操作数),cout是第二个操作数 (右操作数)


cout<<d1转换为cout.operatpr(d1),所以会报错


若想通过cout<<d1来打印,则不能修改上面的运算符重载,因为类对象抢占了第一个数,默认是this,所以无法通过修改运算符重载来实现该语句


此时把该运算符重载写到外面,当作一个函数,不能当成员函数使用,out就会是第一个操作数

13.png

但会有一个缺陷,私有变量无法访问

14.png



这里可以通过友元进行修改

15.png

此时就可以访问这些私有成员


16.png


但此时这样写又会不支持

17.png

这里需要返回值,返回值就是cout,这样才能连续的插入


18.png


此时可正常打印

19.png20.png21.png


这个函数可能会被频繁调用,可能输出多行,因此搞成内联函数最好

22.png

但是此时声明在.h定义在.cpp,声明和定义分离,链接时报错


23.png


直接在.h里面定义好就行  

相关文章
|
1天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
17 0
|
7天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
27天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
34 0
|
27天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
32 0
|
1天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
5天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
7天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”

热门文章

最新文章