🌟 各位看官好,我是egoist2023!
🌍 种一棵树最好是十年前,其次是现在!
🚀 今天来学习C++类和对象的语法知识。注意:在本章节中,小编会以Date类举例
👍 如果觉得这篇文章有帮助,欢迎您一键三连,分享给更多人哦
目录
引入
运算符重载
赋值运算符重载
日期类实现
日期计算
日期判断
流插入<< 和 流提取>>
const成员函数和取地址运算符重载
引入
//C++中
Date d1(2024, 12, 24);
Date d2(2024, 12, 24);
d1 = d2;
//C语言中
d1._year = d2._year;
d1._month = d2._month;
d1._day = d2_day;
在上面一段代码中,我们想把类类型的d2赋值给d1,如果在C语言版本中是需要把一个一个内置类型拷贝过去,未免过于麻烦。有什么方法能直接让类对象d2赋值给d1呢?在C++中提出了运算符重载的概念,可以为类类型的对象指定新的含义。
运算符重载
- C++规定类类型对象使用运算符时,必须调用对应运算符重载,若没有对应的运算符重载,则会编译报错。
- 运算符重载是一个函数,具有其返回类型和参数列表以及函数体,由operator和运算符共同构成。
//在成员函数中 bool operator=(const Date* d)
- 几元运算符对应有几个运算对象数量。如,一元运算符有一个运算对象,二元运算符有两个运算对象,且规定二元运算符左侧运算对象传给第一个参数,右侧传给第二个参数。(如果没有类类型形参则会报错,在全局函数中 int operator+(int x, int y) )
- 重载<<和>>时,需要重载为全局函数(把ostream/istream放到第一个形参位置,第二个跟类类型对象)。 因为重载为成员函数,this指针默认为第一个形参位置,是左侧运算对象,调用时就变成了对象<<cout,用起来不习惯。
ostream& operator<<(ostream& out, const Date& d); istream& operator>>(istream& in, Date& d);
- 运算符重载不能连接语法中没有的符号,如operator@。 同时注意 .* :: sizeof ?: . 以上5个运算符不能重载。
- 一个类是否需要重载哪些运算符,是看重载后是否有意义,比如Date类重载operator-就有意义(可以算差值多少天)。
- 在C语言中有前置++和后置++,运算符重载函数名都是operator++,为了方便区分。C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,方便区分。
Date& operator++();//前置++ Date operator++(int);//后置++
赋值运算符重载
如下一行代码是否是赋值运算符重载呢?并不是,是拷贝构造。
注意:拷贝构造是把一个已存在的对象拷贝初始化零一个要创建的对象;
而赋值运算符重载是⽤于完成两个已经存在的对象直接的拷贝赋值。
Date d1=d2//d1和d2都是类类型对象
1. 赋值运算符重载 (跟拷贝构造类似)是⼀个运算符重载,必须重载为成员函数。赋值运算重载的参数建议 写成const 当前类类型引用 (减少拷贝)。返回值时也建议携程类类型引用(提高效率,支持返回值可提供连续赋值场景)。Date& operator=(const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; }
2.没有显式实现时,编译器会默认生成,对内置类型成员变量会浅拷贝(像Date类就可以不实现赋值赋值重载函数),对自定义类型成员变量会调用赋值重载函数(如 Stack ,_a指向了资源,默认生成的赋值重载函数不符合需求,需要 自己实现深拷贝 也对指向的资源也进行拷贝)。3.同样,对于MyQueue这样的类型内部主要是自定义Stack成员,编译器默认生成赋值运算符重载会调用Stack的赋值运算符重载。
日期类实现
构造函数 | Date(int year = 1, int month = 1, int day = 1) |
获取日期 | int GetMonthDay(int year, int month) const |
日期是否合法 | bool checkDate() const |
打印日期 | void Print() |
日期类构造函数不过多介绍。实现代码如下代码所示
//构造函数
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!checkDate())
{
cout << "日期非法:<" << *this << endl;
}
}
已知某年某月,如何知道是何日呢? 由于该函数需要频繁调用,故设在类里面,类里默认为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))
{
return 29;
}
return MonthDayArray[month];
}
如果输入的年月日 不符合实际上的年月日,需要一个函数检查是否合法。
bool Date::checkDate() const
{
//非法返回false
if (_month < 1 || _month>12 || _day > GetMonthDay(_year, _month) || _day < 1)
return false;
else
return true;
}
日期计算
日期+天数 | Date operator+(int day) const |
日期+=天数 | Date& operator+=(int day) |
日期-天数 | Date& operator-=(int day) |
日期-=天数 | Date operator-(int day) const |
日期-日期 | int operator-(const Date& d) const |
前置/后置++ | Date& operator++() 前置++ Date operator++(int) 后置++ |
前置/后置-- | Date& operator--() 前置-- Date operator--(int) 后置-- |
那是第一种复用第二种,还是第二种复用第一种呢?我们来剖析下函数的具体行为是什么。
第一种采用的是传值返回,为什么不传引用返回呢?因为返回的是tmp,tmp是建立在这个栈空间的,函数结束后栈会被销毁,因此要用传值返回。而C++规定类传值返回会调用拷贝构造,会有一定的消耗。
第二种采用的是传引用返回,由于类类型d并不是在此栈空间的,传引用返回能减少拷贝,提高效率。
可以发现下图中第二种复用第一种会有5次消耗,而第一种复用第二种只存在2次消耗。
同样日期-天数和日期-=天数也可以采用复用的逻辑。但其中还有借月的逻辑,需要细细来剖析。
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += (-day);
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//优化
Date Date::operator-(int day) const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
由于实现了日期加减天数的函数,前置和后置的加减都可以采用复用的逻辑。 (这里就不过多赘述)
//++d
Date& Date::operator++()
{
//复用
*this += 1;
return *this;
}
//d++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//--d
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//d--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
接下来我们来看看日期-日期的实现方法。
当然,也可以通过一个变量来记录2024年4月5日到2025年3月7日的天数,同样采用复用的逻辑。
int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
return flag * count;
}
日期判断
d1>d2 | bool operator>(const Date& d) const |
d1>=d2 | bool operator>=(const Date& d) const |
d1<d2 | bool operator<(const Date& d) const |
d1<=d2 | bool operator<=(const Date& d) const |
d1==d2 | bool operator==(const Date& d) const |
d1!=d2 | bool operator!=(const Date& d) const |
bool Date::operator>(const Date& d) const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month)
{
return _day > d._day;
}
return true;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
在实现日期判断的时候,通常只要实现两个一个>和==的函数,其他判断只要复用这两个函数就可以实现。如要实现d1>=d2,则只要满足d1>d2或d1==d2;实现d1<d2,则满足!(d1>=d2)。
流插入<< 和 流提取>>
流插入<< | ostream& operator<<(ostream& out, const Date& d) |
流提取>> | istream& operator>>(istream& in, Date& d) |
前面我们说过需要把 流插入<< 和 流提取>> 设置为全局变量,否则不符合我们的使用习惯。
需要注意的是,流插入时类类型d是可以+const,并不改变里面的值,而流提取不可以。
如果我们输入的年月日是存在非法的情况的,因此需要判断。
//流插入
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
//流提取
istream& operator>>(istream& in, Date& d)
{
//in >> d._year >> d._month >> d._day;
while (1)
{
in >> d._year >> d._month >> d._day;
if (d.checkDate())
{
break;
}
else {
cout << "输入日期非法,请重新输入:<" << endl;
}
}
return in;
}
const成员函数和取地址运算符重载
const修饰的成员函数称之为const成员函数,放到成员函数参数列表的后面。void Print() const;
const修饰的是成员函数隐含的this指针,表明在成员函数中不能对类的任何成员进行修改。Date* const this 加const修饰 const Date* const this
取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,一般不需要去显式实现,使用一些特殊情景。如不想让别人取到当前类对象的地址,就可以自己实现,胡乱返回一个地址,调试的时候就会发现地址一直对不上。Date* operator&() { //return this; return (Date*)0x0123753f; } const Date* operator&() const { //return this; return nullptr; }
![]()