一. 重载"==","!=","<","<=",">",">="
以日期建一个类,重写构造函数:
class Date
{
private:
int m_day;
int m_month;
int m_year;
public:
Date(int day, int month, int year)
:m_day(day), m_month(month), m_year(year)
{
}
}
在类中重载“==”,“!=”:
bool operator==(const Date& compareTo)
{
bool bol = m_day == compareTo.m_day && m_month == compareTo.m_month && m_year == compareTo.m_year;
return bol;
}
bool operator!=(const Date& compareTo)
{
bool bol = this->operator==(compareTo);
return !bol;
}
在类中重载"<=",">",">=":
bool operator<(const Date& compareTo)
{
if (m_year< compareTo.m_year)
{
return true;
}
else if (m_month < compareTo.m_month)
{
return true;
}
else if (m_day < compareTo.m_day)
{
return true;
}
else
{
return false;
}
}
bool operator<=(const Date& compareTo)
{
if (this->operator==(compareTo))
{
return true;
}
else
{
bool bol = this->operator<(compareTo);
return bol;
}
}
bool operator>(const Date& compareTo)
{
bool bol = this->operator<=(compareTo);
return !bol;
}
bool operator>=(const Date& compareTo)
{
if (this->operator==(compareTo))
{
return true;
}
else
{
bool bol = this->operator>(compareTo);
return bol;
}
}
下面给出完整代码:
#include <iostream>
using namespace std;
class Date
{
private:
int m_day;
int m_month;
int m_year;
public:
Date(int day, int month, int year)
:m_day(day), m_month(month), m_year(year)
{
}
bool operator==(const Date& compareTo)
{
bool bol = m_day == compareTo.m_day && m_month == compareTo.m_month && m_year == compareTo.m_year;
return bol;
}
bool operator!=(const Date& compareTo)
{
bool bol = this->operator==(compareTo);
return !bol;
}
bool operator<(const Date& compareTo)
{
if (m_year< compareTo.m_year)
{
return true;
}
else if (m_month < compareTo.m_month)
{
return true;
}
else if (m_day < compareTo.m_day)
{
return true;
}
else
{
return false;
}
}
bool operator<=(const Date& compareTo)
{
if (this->operator==(compareTo))
{
return true;
}
else
{
bool bol = this->operator<(compareTo);
return bol;
}
}
bool operator>(const Date& compareTo)
{
bool bol = this->operator<=(compareTo);
return !bol;
}
bool operator>=(const Date& compareTo)
{
if (this->operator==(compareTo))
{
return true;
}
else
{
bool bol = this->operator>(compareTo);
return bol;
}
}
};
int main()
{
Date date1(17, 11, 2023);
Date date2(17,12,2023);
if (date1< date2)
{
cout << "<" << endl;
}
if (date1 > date2)
{
cout <<">" << endl;
}
if (date1 <= date2)
{
cout << "<=" << endl;
}
if (date1 >= date2)
{
cout << ">=" << endl;
}
return 0;
}
二. 重载复制赋值运算符"="
以自己重造string类为例,完整代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class MyString
{
private:
char* buffer;
public:
MyString(const char*initInput)
{
if (initInput!=NULL)
{
buffer = new char[strlen(initInput) + 1];
strcpy(buffer, initInput);
}
else
{
buffer == NULL;
}
}
MyString& operator=(const MyString& copyString)
{
if ((this!=©String)&&(copyString.buffer!=NULL))
{
if (buffer!=NULL)
{
delete[]buffer;
}
buffer = new char[strlen(copyString.buffer) + 1];
strcpy(buffer, copyString.buffer);
}
return *this;
}
operator const char*()
{
return buffer;
}
~MyString()
{
delete[]buffer;
}
};
int main()
{
MyString str1("hello ");
MyString str2(" world");
cout << str1 << str2<<endl;
str2 = str1;
cout << str1 << str2 << endl;
return 0;
}
本例在编写时省略了复制构造函数,旨在减少代码行,但理论上应添加上。
首先检查源和目标是否同一个对象。如果不是,则释放成员 buffer 占用的内存,再重新给它分配足以存储复制源中文本的内存,然后使用 strcpy()进行复制。
注意:如果要创建不允许复制的类,可将复制构造函数和复制赋值运算符都声明为私有的。只需这
样声明(而不提供实现)就足以让编译器在遇到试图复制对象(将对象按值传递给函数或将一个对象赋给另一个对象)的代码时引发错误。
注意:添加#define _CRT_SECURE_NO_WARNINGS是为了解决错误 C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.