美文网首页
C++ 部分运算符重载

C++ 部分运算符重载

作者: gaookey | 来源:发表于2020-08-16 14:01 被阅读0次

可重载的运算符

image.png

不可重载的运算符和符号

image.png

重载运算符为类的成员函数

#include <iostream>
using namespace std;

class MyComplex {
    
private:
    double real, imag;
public:
    MyComplex();
    MyComplex(double d, double i);
    void outComplex();
    MyComplex operator-(const MyComplex &c);
    friend MyComplex operator+(const MyComplex &c1, const MyComplex &c2);
};

MyComplex::MyComplex() {
    real = 0;
    imag = 0;
}

MyComplex::MyComplex(double d, double i) {
    real = d;
    imag = i;
}

void MyComplex::outComplex() {
    cout<<"("<<real<<","<<imag<<")";
}

MyComplex MyComplex::operator-(const MyComplex &c) {
    return MyComplex(this->real - c.real, this->imag - c.imag);
}

MyComplex operator+(const MyComplex &c1, const MyComplex &c2) {
    return MyComplex(c1.real + c2.real, c1.imag + c2.imag);
}

int main() {
    
    MyComplex c1(1, 2), c2(3, 4), res;
    
    c1.outComplex();
    cout<<"operator+";
    c2.outComplex();
    cout<<"=";
    res = c1 + c2;
    res.outComplex();
    cout<<endl;
    
    c1.outComplex();
    cout<<"operator-";
    c2.outComplex();
    cout<<"=";
    res = c1 - c2;
    res.outComplex();
    cout<<endl;
    
    return 0;
}

重载运算符为友元函数

#include <iostream>
using namespace std;

class MyComplex {
    
private:
    double real, imag;
public:
    MyComplex();
    MyComplex(double d, double i);
    void outComplex();
    
    friend MyComplex operator+(const MyComplex &c1, const MyComplex &c2);
    friend MyComplex operator-(const MyComplex &c1, const MyComplex &c2);
    friend MyComplex operator-(const MyComplex &c1, double d);
    friend MyComplex operator-(double d, const MyComplex &c1);
};

MyComplex::MyComplex() {
    real = 0;
    imag = 0;
}

MyComplex::MyComplex(double d, double i) {
    real = d;
    imag = i;
}

void MyComplex::outComplex() {
    cout<<"("<<real<<","<<imag<<")";
}

MyComplex operator+(const MyComplex &c1, const MyComplex &c2) {
    return MyComplex(c1.real + c2.real, c1.imag + c2.imag);
}

MyComplex operator-(const MyComplex &c1, const MyComplex &c2) {
    return MyComplex(c1.real - c2.real, c1.imag - c2.imag);
}

MyComplex operator-(const MyComplex &c1, double d) {
    return MyComplex(c1.real - d, c1.imag);
}

MyComplex operator-(double d, const MyComplex &c1) {
    return MyComplex(d - c1.real, -c1.imag);
}

int main() {
    
    MyComplex c1(1, 2), c2(3, 4), res;
    
    c1.outComplex();
    cout<<"operator+";
    c2.outComplex();
    cout<<"=";
    res = c1 + c2;
    res.outComplex();
    cout<<endl;
    
    res = c1 - 5;
    res.outComplex();
    cout<<endl;
    
    res = 5 - c1;
    res.outComplex();
    cout<<endl;
    
    return 0;
}

重载赋值运算符

#include <iostream>
using namespace std;

class MyComplex {
    
private:
    double real, imag;
public:
    MyComplex();
    MyComplex(double d, double i);
    void outComplex();
    
    friend MyComplex operator+(const MyComplex &c1, const MyComplex &c2);
    friend MyComplex operator+(const MyComplex &c1, double d);
    friend MyComplex operator+(double d, const MyComplex &c2);
    friend MyComplex operator-(const MyComplex &c1, const MyComplex &c2);
    friend MyComplex operator-(const MyComplex &c1, double d);
    friend MyComplex operator-(double d, const MyComplex &c2);
    
    MyComplex &operator=(const MyComplex &c1);
    MyComplex &operator=(double);
};

MyComplex::MyComplex() {
    real = 0;
    imag = 0;
}

MyComplex::MyComplex(double d, double i) {
    real = d;
    imag = i;
}

void MyComplex::outComplex() {
    cout<<"("<<real<<","<<imag<<")";
}

MyComplex operator+(const MyComplex &c1, const MyComplex &c2) {
    return MyComplex(c1.real + c2.real, c1.imag + c2.imag);
}

MyComplex operator+(const MyComplex &c1, double d) {
    return MyComplex(c1.real + d, c1.imag);
}

MyComplex operator+(double d, const MyComplex &c2) {
    return MyComplex(d + c2.real, c2.imag);
}

MyComplex operator-(const MyComplex &c1, const MyComplex &c2) {
    return MyComplex(c1.real - c2.real, c1.imag - c2.imag);
}

MyComplex operator-(const MyComplex &c1, double d) {
    return MyComplex(c1.real - d, c1.imag);
}

MyComplex operator-(double d, const MyComplex &c2) {
    return MyComplex(d - c2.real, -c2.imag);
}

MyComplex& MyComplex::operator=(const MyComplex &c1) {
    this->real = c1.real;
    this->imag = c1.imag;
    return *this;
}

MyComplex& MyComplex::operator=(double d) {
    this->real = d;
    this->imag = 0;
    return *this;
}

int main() {
    
    MyComplex c1(1, 2), c2(3, 4), res;
    
    c1.outComplex();
    c2.outComplex();
    res = c1 + c2;
    res.outComplex();
    
    res = c1;
    res.outComplex();
    
    return 0;
}

重载流插入运算符和流提取运算符

#include <iostream>
using namespace std;

class Test {
    
private:
    int i;
    float f;
    char ch;
public:
    Test(int a = 0, float b = 0, char c = '\0') {
        i = a;
        f = b;
        ch = c;
    }
    
    friend ostream &operator<<(ostream &, Test);
    friend istream &operator>>(istream &, Test &);
};

ostream &operator<<(ostream &stream, Test test) {
    stream<<test.i<<",";
    stream<<test.f<<",";
    stream<<test.ch<<endl;
    return stream;
}

istream &operator>>(istream &stream, Test &test) {
    stream>>test.i;
    stream>>test.f;
    stream>>test.ch;
    return stream;
}

int main() {
    
    Test a(33, 8.8, 'a');
    operator<<(cout, a);
    
    Test b, c;
    operator>>(cin, b);
    operator>>(cin, c);
    
    operator<<(cout, b);
    operator<<(cout, c);
    
    return 0;
}

重载自增/自减运算符

#include <iostream>
using namespace std;

class Test {
    
private:
    int n;
public:
    Test(int i = 0) : n(i) { }
    
    Test &operator++();
    Test operator++(int);
    
    operator int() { return n; }
    
    Test &operator--();
   friend Test operator--(Test &, int);
};

Test &Test::operator++() {
    n ++;
    return *this;
}

Test Test::operator++(int) {
    Test temp(*this);
    n ++;
    return temp;
}

Test &Test::operator--() {
    n --;
    return *this;
}

Test operator--(Test &d, int) {
    Test temp(d);
    d.n --;
    return temp;
}

int main() {
    
    Test d(10);
    cout<<(d++)<<endl;
    cout<<d<<endl;
    
    return 0;
}

相关文章

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++ 运算符重载

    运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。实际上,很多C++运算符已经重载。将*运算符用...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • 2017 09 22

    Am:对运算符重载进行学习: 基础定义部分: 1.重载定义:根据自己的需要对C++已提供的运算符进行重载,赋予新的...

  • C++运算符重载-下篇 (Boolan)

    C++运算符重载-下篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++运算符重载-上篇 (Boolan)

    C++运算符重载-上篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++ 重载运算符

    C++重载运算符

  • C++重载

    重载 C++语言规定: 重载的运算符要保持原运算符的意义。只能对已有的运算符重载,不能增加新的运算符。重载的运算符...

  • 1.2.17_C++ ++ 和 -- 运算符重载

    C++ 重载运算符和重载函数 递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符...

网友评论

      本文标题:C++ 部分运算符重载

      本文链接:https://www.haomeiwen.com/subject/kcvtjktx.html