C++之重载运算符

一. 重载"==","!=","<","<=",">",">="

以日期建一个类,重写构造函数:

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!=&copyString)&&(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用虚重载运算符来实现多态性。虚重载运算符是指在基类中定义的虚函数,在派生类中可以被重载的运算符。例如,可以在基类中定义一个虚函数operator+,然后在派生类中重载这个运算符。 以下是一个示例代码,其中基类为Shape,派生类为Rectangle和Circle,它们都重载了运算符+: ``` #include <iostream> using namespace std; class Shape { public: virtual double operator+(Shape& other) { return 0; } }; class Rectangle : public Shape { public: double width, height; Rectangle(double w, double h) : width(w), height(h) {} virtual double operator+(Shape& other) { Rectangle& r = dynamic_cast<Rectangle&>(other); return width * height + r.width * r.height; } }; class Circle : public Shape { public: double radius; Circle(double r) : radius(r) {} virtual double operator+(Shape& other) { Circle& c = dynamic_cast<Circle&>(other); return 3.14 * radius * radius + 3.14 * c.radius * c.radius; } }; int main() { Rectangle r(3, 4); Circle c(5); Shape* s1 = &r; Shape* s2 = &c; cout << (*s1 + *s2) << endl; // 输出 97.86(3*4+3.14*5*5) return 0; } ``` 在上面的代码中,Shape类中定义了一个虚函数operator+,Rectangle和Circle类都重载了这个运算符,并且使用了dynamic_cast进行类型转换,以便访问派生类的成员变量。在main函数中,分别创建了一个Rectangle对象和一个Circle对象,并将它们转换为Shape指针,然后调用它们的运算符+,得到了正确的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值