前言
本系列文章承接C语言的学习,需要有C语言的基础才能学会哦~
第6篇主要讲的是有关于C++类的默认成员函数中的赋值运算符重载,以及使用C++实现日期类。
C++才起步,都很简单呢!
!!书接上回!!--->从C++开始的编程生活(5)——类的默认成员函数中的构造函数、析构函数、拷贝构造函数和运算符重载
赋值运算符重载
也属于默认成员函数。与拷贝构造不同,拷贝构造适用于已经存在对象拷贝初始化给一个新的对象,是构造行为。
基本语法
Date& operator=(const Date& d)
{
if(this != &d)//避免自己赋值自己
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//注意!d1和d3已经完成初始化,否则会调用拷贝构造
d1 = d3;
//相当于
d1.operator=(d3)
//可支持连续赋值
d1 = d2 = d3
//如果operter=的返回值是void,就不可以连续传参
特点
①是运算符重载,必须重载为成员函数,参数写为const当前类类型引用,减少传值传参的拷贝。
②有返回值,且建议写成当前类类类型引用。引用返回提高效率,有返回值可实现连续赋值。
③若没有显式实现,编译器自动生成一个默认赋值运算符重载,行为同拷贝构造类似(见前文拷贝函数部分)。也就是说如果成员都是内置类型且没有指针和引用,可以不显式实现,直接浅拷贝,否则就要自己实现完成深拷贝(对指向的资源也拷贝)。
④由③可知,如果一个类显式实现了析构或者构造时申请了资源,那么就必须自行显式实现赋值运算符重载。
实现日期类
仅供参考,可自行测试。
Date.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
//友员
friend istream& operator>>(istream& in, Date& d);
friend ostream& operator<<(ostream& out, const Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void Print();
bool CheckDate()
{
if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
//判断闰年2月
if ( month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
return monthDayArray[month];
}
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+(int day);
Date& operator-=(int day);
Date operator-(int day);
//前置++
Date& operator++();
//后置++
Date operator++(int);
//前置--
Date& operator--();
//后置--
Date operator--(int);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
//ostream& 返回值可连续赋值
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp
#include"Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate)
{
cout << "日期非法! -> ";
cout << *this;
}
}
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
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;
}
return false;
}
bool Date::operator==(const Date& d)
{
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 || *this == d;
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
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)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;//复用operator+=,如果用+=复用+,会有拷贝过程,效率低
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
return *this += -day;
_day -= day;
while(_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp += day;
return tmp;
}
//前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
//不加const,需要访问d
istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请依次输入年月日:";
in >> d._year >> d._month >> d._day;
if (d.CheckDate())
{
break;
}
else
{
cout << "日期非法,请重新输入" << endl;
}
}
return in;
}
tips:运算符重载可以尽可能多的进行相互调用用,可以提升代码可读性,提高效率。如+重载可以调用+=来实现。
❤~~本文完结!!感谢观看!!接下来更精彩!!欢迎来我博客做客~~❤