C++ 运算符重载的使用

运算符重载:
在C++中要使用运算符重载,需要使用被称为运算符重载的特殊函数形式。运算符重载的函数的格式如下:

operatorop(argument-list);

以时间相加的函数为例:
1:常规的加法运算符操作:
头文件:

#pragma once
class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m=0);
	void Addmin(int m);
	void AddHr(int h);
	void Reset(int h=0,int m=0);
	Time Sum(const Time& t) const;
	void Show() const;
};

源文件:

#include "mytime0.h"
#include <iostream>

Time::Time()
{
	hours = minutes = 0;
}

Time::Time(int h, int m)
{
	hours = h;
	minutes = m;
}

void Time::Addmin(int m)
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}

Time Time::Sum(const Time & t) const
{
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

void Time::Show() const
{
	std::cout << hours << " hours " << minutes << " minutes"<<std::endl;
}

程序测试:


#include <iostream>
#include "mytime0.h"
using  namespace std;

int main()
{
	using std::count;
	using std::endl;
	Time planning;
	Time coding(2,40);
	Time fixing(5,55);
	Time total;

	cout << "planning time= " ;
	planning.Show();
	cout << endl;

	cout << "coding time= ";
	coding.Show();
	cout << endl;

	cout << "fixing time= ";
	fixing.Show();
	cout << endl;

	total = coding.Sum(fixing);
	cout << "coding.Sum(fixing)= ";
	total.Show();
	cout << endl;

	return 0;
}

程序输出:

planning time= 0 hours 0 minutes
coding time= 2 hours 40 minutes
fixing time= 5 hours 55 minutes
coding.Sum(fixing)= 8 hours 35 minutes

重载加法运算符:
只需要将Sum()的名称改为operator+()即可:
头文件:

#pragma once
class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m=0);
	void Addmin(int m);
	void AddHr(int h);
	void Reset(int h=0,int m=0);
	Time operator+(const Time& t) const;
	void Show() const;
};

源文件:

#include "mytime0.h"
#include <iostream>

Time::Time()
{
	hours = minutes = 0;
}

Time::Time(int h, int m)
{
	hours = h;
	minutes = m;
}

void Time::Addmin(int m)
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}

Time Time::operator+(const Time & t) const
{
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

void Time::Show() const
{
	std::cout << hours << " hours " << minutes << " minutes"<<std::endl;
}

和Sum()一样,operator+()也是由Time对象调用,它将第二个Time对象作为参数,并返回一个Time对象。因此,可以像调用Sum()那样来调用operator+() 方法:
total=coding.operator+(fixing);
该方法也可以直接使用运算符方法+:
total=coding + fixing;
这两种使用方法最终的输出结果是相同的。
使用运算符重载的函数名称可以使用函数表示法或运算符表示法来调用它,编译器将根据操作数的类型来确定如何做。

### 结构体中的运算符重载C++ 中,结构体(`struct`)同样支持运算符重载功能。通过定义特定的成员函数或者友元函数,可以使自定义类型的对象像基本数据类型那样参与各种运算。 #### 定义加法运算符用于两个向量相加 当希望实现类似于 `Vector v3 = v1 + v2;` 的语句时,可以在结构体内定义一个名为 `operator+` 的成员函数: ```cpp #include <iostream> struct Vector { int x, y; // 构造函数初始化列表 Vector(int a = 0, int b = 0): x(a), y(b) {} // 加法运算符重载 Vector operator+(const Vector& vec) const { return Vector(x + vec.x, y + vec.y); } }; int main() { Vector v1(1, 2), v2(3, 4); Vector result = v1 + v2; std::cout << "(" << result.x << ", " << result.y << ")" << std::endl; } ``` 上述代码展示了如何创建一个简单的二维向量类,并实现了两者的加法操作[^1]。 #### 实现赋值运算符 对于更复杂的场景,比如深拷贝等情况,则可能需要用到赋值运算符(`=`),其一般形式如下所示: ```cpp struct MyClass { private: int* data; public: MyClass(const MyClass &other); // 拷贝构造函数 ~MyClass(); // 析构函数 // 赋值运算符重载 MyClass& operator=(const MyClass& other){ if (this != &other){ // 防止自我赋值 delete[] this->data; // 清理原有资源 size_t length = strlen(other.data)+1; this->data = new char[length]; strncpy(this->data, other.data, length); // 或者复制其他成员变量... } return *this; } }; ``` 此段代码说明了怎样安全地处理动态分配内存的对象之间的赋值过程[^2]。 #### 自增/自减运算符 除了常见的二元运算符外,还可以为一元前缀和后缀版本的一元运算符提供不同的行为模式。例如,下面的例子演示了一个计数器结构体中前后置递增的区别: ```cpp struct Counter { unsigned count{0}; // 前置++ Counter& operator++(){ ++count; return *this; } // 后置++, 参数i用来区分前置与后置的形式 Counter operator++(int i){ Counter temp(*this); ++count; return temp; } }; ``` 这段程序解释了一种方式来区别对待前置增量表达式 (`++counter`) 和后置增量表达式 (`counter++`) 的不同之处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值