c++ 重载+运算符
时间: 2023-12-03 09:05:53 浏览: 37
C++ 中的运算符重载是指给已有的运算符赋予新的功能或含义。通过重载运算符,我们可以定义自定义的数据类型在使用运算符时的行为。
要重载一个运算符,需要使用特定的函数名和语法。以下是一些常见的运算符以及对应的函数名:
- 一元运算符:重载时只需要一个参数
- 一元正号:`+` ==> `operator+`
- 一元负号:`-` ==> `operator-`
- 逻辑非:`!` ==> `operator!`
- 二元运算符:重载时需要两个参数
- 加法:`+` ==> `operator+`
- 减法:`-` ==> `operator-`
- 乘法:`*` ==> `operator*`
- 除法:`/` ==> `operator/`
- 取余:`%` ==> `operator%`
- 等于:`==` ==> `operator==`
- 不等于:`!=` ==> `operator!=`
- 赋值运算符:重载时需要两个参数
- 赋值:`= ` ==> `operator=`
- 加法赋值:`+=` ==> `operator+=`
- 减法赋值:`-=` ==> `operator-=`
- 乘法赋值:`*=` ==> `operator*=`
- 除法赋值:`/=` ==> `operator/=`
除了以上列举的运算符,C++ 还提供了其他一些运算符可以进行重载。在重载运算符时,需要注意运算符的语义和预期行为,以及遵循一些重载运算符的约定和规则。
相关问题
c++电子时钟,重载+运算符和-运算符
下面是一个基本的 C++ 电子时钟实现,其中重载了 + 和 - 运算符:
```c++
#include <iostream>
using namespace std;
class Clock {
private:
int hour, minute, second;
public:
Clock(int h = 0, int m = 0, int s = 0) {
hour = h;
minute = m;
second = s;
}
void display() {
cout << hour << ":" << minute << ":" << second << endl;
}
Clock operator+(const Clock& c) const {
int h = hour + c.hour;
int m = minute + c.minute;
int s = second + c.second;
if (s >= 60) {
s -= 60;
m++;
}
if (m >= 60) {
m -= 60;
h++;
}
if (h >= 24) {
h -= 24;
}
return Clock(h, m, s);
}
Clock operator-(const Clock& c) const {
int h = hour - c.hour;
int m = minute - c.minute;
int s = second - c.second;
if (s < 0) {
s += 60;
m--;
}
if (m < 0) {
m += 60;
h--;
}
if (h < 0) {
h += 24;
}
return Clock(h, m, s);
}
};
int main() {
Clock c1(10, 30, 45), c2(12, 50, 15);
cout << "c1 = ";
c1.display();
cout << "c2 = ";
c2.display();
Clock c3 = c1 + c2;
cout << "c3 = ";
c3.display();
Clock c4 = c2 - c1;
cout << "c4 = ";
c4.display();
return 0;
}
```
在上面的代码中,Clock 类具有小时、分钟和秒属性,并且有一个默认构造函数。display() 函数用于显示当前时间。operator+() 和 operator-() 分别重载了 + 和 - 运算符,以使 Clock 对象可以相加和相减。
在主函数中,我们创建了两个 Clock 对象 c1 和 c2,并显示了它们的值。然后我们通过重载的 + 运算符将它们相加,将结果存储在 c3 中,并将其显示出来。接下来,我们通过重载的 - 运算符将 c2 从 c1 中减去,将结果存储在 c4 中,并将其显示出来。
c++高精度++运算符重载
C++中的高精度运算符重载是指对整数进行大数运算时,通过重载运算符来实现对大数的加减乘除等操作。一般情况下,C++内置的整数类型(如int、long等)有一定的位数限制,无法处理超过其表示范围的大数。而通过运算符重载,我们可以自定义一个类来表示大数,并对其进行各种运算操作。
以下是一个简单的示例,展示了如何实现C++中的高精度运算符重载:
```cpp
#include <iostream>
#include <vector>
using namespace std;
class BigInteger {
private:
vector<int> digits; // 用vector存储大数的每一位
public:
BigInteger() {}
BigInteger(int num) {
while (num > 0) {
digits.push_back(num % 10);
num /= 10;
}
}
BigInteger operator+(const BigInteger& other) const {
BigInteger result;
int carry = 0;
int i = 0;
while (i < digits.size() || i < other.digits.size() || carry != 0) {
int sum = carry;
if (i < digits.size()) {
sum += digits[i];
}
if (i < other.digits.size()) {
sum += other.digits[i];
}
result.digits.push_back(sum % 10);
carry = sum / 10;
i++;
}
return result;
}
friend ostream& operator<<(ostream& os, const BigInteger& num) {
for (int i = num.digits.size() - 1; i >= 0; i--) {
os << num.digits[i];
}
return os;
}
};
int main() {
BigInteger a(123456789);
BigInteger b(987654321);
BigInteger c = a + b;
cout << "a + b = " << c << endl;
return 0;
}
```
在上述示例中,我们定义了一个名为BigInteger的类,用于表示大数。通过重载加法运算符`+`,我们可以实现对两个BigInteger对象的相加操作。同时,我们还重载了输出流运算符`<<`,以便能够直接输出BigInteger对象的值。