CPP-operator==, <overloading

这篇博客探讨了在C++中如何利用已有的运算符重载来实现其他相关操作符。一旦定义了`operator==`,可以轻松实现`operator!=`,只需调用`operator==`并取反。同样,定义了`<`后,可以方便地定义`>`。标准库提供了一些模板帮助实现这些关系操作符。文章通过为数组类型定义`<`运算符为例,展示了如何实现这些操作,并提到了在成员函数中使用`this`指针的细节。然而,在尝试使用非常量成员函数(如lengthsquare())时,由于`operator<`是常量成员函数,导致了编译错误。
摘要由CSDN通过智能技术生成

如果已经定义了operator==,那么operator!=很容易实现,只要调用operator==,然后取反。

bool Array::operator!=(const Array& v) const
{
    return !((*this) == v);
}

这个函数的形式实际上和具体的类无关。即如果定义了operator==,那么总能根据这个模式定义operatot!=. 同样,如果定义了<, 则很容易能够定义>. 如果定义了==,则结合<和==,则能够定义<=和>=. 标准文件util包含了一系列预先定义的这些关系操作符的模式。使用时只需定义operator==, operator<,则自动有operator!=, operator>, operator<=.因此从不需要定义多余两个比较操作符。看以下例子:为array定义<.

bool operator< (const Array& v) const;

实现:

bool Array::operator< (const Array& v) const
{
    int n; //the length of the shortest of this object and v
    if(num <v.num)
    n = num;
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.0 6.0 (complex numbers C1=1.0+2.0i, C2=3.0+4.0i, integer num=5) Output format: Please write the output format here. For example: 1.0+2.0i + 3.0+4.0i = 4.0+6.0i Sure, I'd be happy to help you with that! Here's an example program in C++ that overloads the +, -, *, and / operators for complex numbers: ```cpp #include <iostream> using namespace std; class Complex { double real, imag; public: Complex(double r = 0, double i = 0) { real = r; imag = i; } Complex operator + (const Complex& c) const { return Complex(real + c.real, imag + c.imag); } Complex operator - (const Complex& c) const { return Complex(real - c.real, imag - c.imag); } Complex operator * (const Complex& c) const { return Complex(real*c.real - imag*c.imag, real*c.imag + imag*c.real); } Complex operator / (const Complex& c) const { double r = c.real*c.real + c.imag*c.imag; return Complex((real*c.real + imag*c.imag) / r, (imag*c.real - real*c.imag) / r); } Complex operator + (const int num) const { return Complex(real + num, imag); } Complex operator - (const int num) const { return Complex(real - num, imag); } Complex operator * (const int num) const { return Complex(real*num, imag*num); } Complex operator / (const int num) const { return Complex(real/num, imag/num); } friend ostream& operator<<(ostream& os, const Complex& c); }; ostream& operator<<(ostream& os, const Complex& c) { os << c.real << "+" << c.imag << "i"; return os; } int main() { Complex C1(1.0, 2.0); Complex C2(3.0, 4.0); int num = 5; // Addition cout << C1 << " + " << C2 << " = " << C1 + C2 << endl; cout << C1 << " + " << num << " = " << C1 + num << endl; cout << num << " + " << C1 << " = " << num + C1 << endl; // Subtraction cout << C1 << " - " << C2 << " = " << C1 - C2 << endl; cout << C1 << " - " << num << " = " << C1 - num << endl; cout << num << " - " << C1 << " = " << num - C1 << endl; // Multiplication cout << C1 << " * " << C2 << " = " << C1 * C2 << endl; cout << C1 << " * " << num << " = " << C1 * num << endl; cout << num << " * " << C1 << " = " << num * C1 << endl; // Division cout << C1 << " / " << C2 << " = " << C1 / C2 << endl; cout << C1 << " / " << num << " = " << C1 / num << endl; cout << num << " / " << C1 << " = undefined" << endl; // division by complex number is undefined return 0; } ``` In this program, we define a class `Complex` to represent complex numbers. We overload the +, -, *, and / operators both for complex numbers and for an integer. We also define a `friend` function to overload the << operator to print complex numbers in a readable format. To use this program, you can simply copy and paste the code into a C++ compiler, and run it. You will be prompted to enter the complex numbers and integer as input, and the program will output the results of the addition, subtraction, multiplication, and division operations. I hope this helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值