欢迎大家再次来到海盗猫鸥的博客——
今天将继续讲解类和对象的后续内容,本篇将讲解类和对象中运算符重载,赋值运算符重载,以及取地址运算符的内容,再结合内容进行Date日期类的实现。
目录
运算符重载
运算符重载
赋值运算符重载
取地址运算符重载
const成员函数
取地址运算符重载
Date日期类的实现
Date.h头文件:
Date.cpp文件
补充
结语
运算符重载
当运算符被用于非内置类型时,可能将导致报错。而C++允许通过运算符重载的形式来指定运算符新的含义,从而达到处理自定义类型的功能。
运算符重载
- 运算符重载实际就是拥有特殊名字的函数,由operator加上需要重定义的运算符共同构成;和普通的函数一样,拥有返回值,参数和函数体。
- 重载运算符函数的参数个数和该运算符的操作对象数量相同,即一元运算符只有一个参数,二元运算符有两个参数,二元运算符的左操作数传给第一个参数,有操作数传给第二个参数。
- 如果重载运算符函数是一个成员函数,那么他的第一个运算对象默认传递给隐式的this指针,所以运算符重载作为成员函数时,参数会比运算符操作数少一个。
- 运算符重载不改变运算符的优先级和结合性。
- 只能重载语法中与原本就存在的运算符,不能用来创建新的运算符。如:operator@
- .* :: sizeof ? : . 这五个运算符不能重载
- 重载运算符至少有一个自定义类型的参数,运算符对内置类型的运算规则,无法通过重载来改变
- 一个类需要重载哪些运算符,是看哪些运算符重载后对于这个类有意义
- 重载自增++运算符时,由于有前置和后置的区别,但运算符重载时函数名都为operator++,为了区分,在后置++重载时,添加一个int形参(该参数不接收内容,只用于区分),由此来和前置++的构成重载函数。
- 重载流插入<<和流提取>>时,需要重载为全局函数,因为成员函数的第一个参数默认为类对象的this指针,此时调用函数,就变成了 对象<<cout 或者 对象 >> cin ;不符合使用习惯,所以床在为全局函数,将istream/ostream放到第一个参数位置,将对象放到第二个参数位置。
//类类型流输出
ostream& Date::operator<<(ostream& out)
{//定义为成员函数,第一个参数位置将被this指针占用,导致重载<<的左右操作数相对于原本的参数位置颠倒out << _year << "年" << _month << "月" << _day << "日" << endl;return out;
}
ostream& operator<<(ostream& out, const Date& d)
{//全局函数不能访问类私有成员,需要进行友元声明out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
赋值运算符重载
赋值运算符重载是一个默认成员函数,当没有显示定义时,编译器器会自动生成,注意和拷贝构造区分。
- 赋值运算符重载必须重载为成员函数,参数建议写为const的类类型引用,减少拷贝;
- 返回值为左操作数,即第一个参数,使用引用返回提高效率;
- 未显示定义时编译器会自动生成默认赋值运算符重载,和默认拷贝构造函数相似,只会对内置类型进行浅拷贝/值拷贝,自定义类型则调用它的赋值重载。
- 类中没有资源的使用时,默认的重载函数就可以满足要求,当的类的成员变量有指向资源时,就需要显示定义其功能。
取地址运算符重载
const成员函数
-
将const修饰的成员函数称为const成员函数,const放在函数参数列表的后面
void Print() 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;bool operator!=(const Date& d) const;
- const实际修饰的时隐含的this指针,使该this指针指向的对象在该函数体内不可修改,Date类为例原本this指针为:Date* const this,而const成员函数的this为:const Date* const this;
- 当成员函数不涉及修改this指针的对象时,都建议设置为const成员函数,提高安全性。
取地址运算符重载
取地址运算符重载有普通取地址运算符重载和const取地址运算符重载,但一般由编译器默认生成的就能满足需求。
Date* operator&()
{
return this;
// return nullptr;
}
const Date* operator&()const
{
return this;
// return nullptr;
}
Date日期类的实现
Date.h头文件:
由于成员变量都为内置类型的变量,所以拷贝构造以及析构函数都可以不写;
GetMonthDay函数用于获取当年当月的天数,调用频繁且函数简短,设计为inline内联函数效率更高,此处自动为inline内联函数。
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl; class Date
{//声明友元函数,使全局函数也能使用类私有成员friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public://全缺省构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;if (!CheckDate()){cout << "日期错误" << endl;Print();}}//获取月份天数(常调用的简短函数设计为inline函数)int GetMonthDay(int year, int month) const{//定义为静态数组,防止每次调用都重新创建数组导致效率降低static int MonthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//闰年二月if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))//先判断是否为2月,可以提高效率{return 29;}return MonthDayArray[month];}void Print() const{cout << _year << "年" << _month << "月" << _day << "日" << endl;}//判断日期是否正确bool CheckDate() const;Date& operator=(const Date& d);Date operator+(int day) const;Date operator-(int day) const;Date& operator+=(int day);Date& operator-=(int day);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;//前置++Date& operator++();//后置++,添加一个int形参以示区分,没有实际作用Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//日期 - 日期 = 天数int operator-(const Date& d) const;//ostream& operator<<(ostream& out);//取地址运算符重载/*Date* operator&();const Date* operator&() const;*/private:int _year;int _month;int _day;
};
Date.cpp文件
用于实现各个运算符重载函数;
注意函数间可以进行复用,从而简洁的实现相关的函数,减少不必要的重复代码书写。
#include "Date.h"
bool Date::CheckDate() const
{if ((_month < 1 || _month > 12) || (_day < 1 || _day > GetMonthDay(_year, _month))){return false;}return true;
}Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}//返回对象为左操作数return *this;
}Date Date::operator+(int day) const
{Date tmp = *this;//tmp._day += day;//while (tmp._day > GetMonthDay(tmp._year, tmp._month))//{// tmp._day -= GetMonthDay(tmp._year, tmp._month);// tmp._month++;// if (tmp._month == 13)// {// tmp._year++;// tmp._month = 1;// }//}tmp += day;return tmp;
}
Date Date::operator-(int day) const
{Date tmp = *this;//tmp._day -= day;//while (tmp._day <= 0)//{// //// tmp._month--;// if (tmp._month == 0)// {// tmp._year--;// tmp._month = 12;// }// tmp._day += GetMonthDay(tmp._year, tmp._month);// //}tmp -= day;return tmp;
}
Date& Date::operator+=(int day)
{if (day < 0){*this -= (-day);return *this;}_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)
{//*this = *this - day//复用operator-,但效率相对低if (day < 0){*this += (-day);return *this;}_day -= day;while (_day <= 0){//_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}bool Date::operator<(const Date& d) const
{if (_year < d._year){return true;}else if (_year == d._year){if (_month < d._month){return true;}else if (_month == d._month){if (_day < d._day){return true;}//return _day < d._day;}}return false;
}
bool Date::operator<=(const Date& d) const
{//if (_year <= d._year && _month <= d._month && _day <= d._day)//{// return true;//}//return false;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
{//if (d._year == _year && d._month == _month && d._day == _day)//{// return true;//}//return false;return d._year == _year && d._month == _month && d._day == _day;
}
bool Date::operator!=(const Date& d) const
{//if (d._year == _year && d._month == _month && d._day == _day)//{// return false;//}//return true;return !(*this == d);
}//前置++
Date& Date::operator++()
{*this += 1;//返回++之后的值return *this;
}
//后置++,添加一个int形参以示区分,没有实际作用
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) const
{int n = 0;int flag = 1;Date max = *this;Date min = d;if (*this < d){max = d;min = *this;flag = -1;}while (min != max){++min;++n;}return n * flag;
}//类类型流输出
//ostream& Date::operator<<(ostream& out)
//{
// //定义为成员函数,第一个参数位置将被this指针占用,导致重载<<的左右操作数相对于原本的参数位置颠倒
// out << _year << "年" << _month << "月" << _day << "日" << endl;
//
// return out;
//}
ostream& operator<<(ostream& out, const Date& d)
{//全局函数不能访问类私有成员,需要进行友元声明out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请输入日期>" << endl;in >> d._year >> d._month >> d._day;if (!d.CheckDate()){cout << "非法日期,请重新输入:" << endl;in >> d._year >> d._month >> d._day;}else{break;}}return in;
}//Date* Date::operator&()
//{
// return this;
//}
//const Date* Date::operator&() const
//{
// return this;
//}
注意:在使用自增和自减时,在保证正确性的前提下建议尽量使用前置自增和自减,因为在重载自增和自减函数时,前置自增自减没有拷贝的过程,效率更高。
补充
cout为ostream类型的对象,cin是istream类型的对象,内置类型之所以能直接使用<<流插入和>>流提取,就是因为默认已经重载好了相关的函数,所以我们重载时原理时相似的。
以流插入<<为例:
结语
本篇博客到此结束,感谢大家的阅读,下篇将是类和对象的最后一篇博客——
个人主页:海盗猫鸥-CSDN博客
本期专栏:C++_海盗猫鸥的博客-CSDN博客
感谢大家关注~