閱讀51 返回首頁    go 阿裏雲 go 技術社區[雲棲]


C++上機實驗三:運算符重載

#include <iostream>
using namespace std;

#ifndef CCOMPLEX_H
#define CCOMPLEX_H

class CComplex
{
    public:
        CComplex(float r = 0.0f, float i = 0.0f): real(r), imag(i)
        {
            cout << "默認構造函數調用\n";
        }
        CComplex(const CComplex & src);
        virtual ~CComplex()
        {
            cout << "虛析構函數調用!\n";
        }
        CComplex & operator[](int index);

        CComplex & operator=(const CComplex & src);

        CComplex  operator*(const CComplex & src);

        CComplex  operator+(const CComplex & src);

        CComplex  operator-(const CComplex & src);

        CComplex  operator/(const CComplex & src);

        CComplex  operator++();//前置自增

        CComplex  operator++(int x);//後置自增

        CComplex  operator--();//前置自減

        CComplex  operator--(int x);//後置自減

        CComplex  operator()(float r, float i);//重載圓括號

        CComplex  operator()(float r);//重載圓括號

        friend bool operator==(const CComplex & dest, const CComplex & src);

        friend bool operator!=(const CComplex & dest, const CComplex & src);

        friend ostream & operator<<(ostream &out, const CComplex & src);

        friend istream & operator>>(istream &in, CComplex & src);




    private:
        float real;
        float imag;
};

#endif // CCOMPLEX_H




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


CComplex::CComplex(const CComplex & src)
{
    real = src.real;
    imag = src.imag;
    cout << "複製構造函數調用\n";
}

//-----------------------------重載=-------------------------
CComplex & CComplex::operator=(const CComplex & src)
{
    real = src.real;
    imag = src.imag;
    cout << "重載賦值運算符調用!\n";

    return (*this);
}

//----------------------------- 重載* -------------------------
CComplex  CComplex::operator*(const CComplex & src)
{
    CComplex tmp(real * src.real - imag * src.imag,
                 real * src.real + imag * src.imag);
    return tmp;
}

//----------------------------- 重載+ -------------------------
CComplex  CComplex::operator+(const CComplex & src)
{
    CComplex tmp(real+src.real, imag+src.imag);
    return tmp;
}

//----------------------------- 重載- -------------------------
CComplex  CComplex::operator-(const CComplex & src)
{
    CComplex tmp(real-src.real, imag-src.imag);
    return tmp;
}

//----------------------------- 重載/ -------------------------
CComplex  CComplex::operator/(const CComplex & src)
{
    float tmp =  src.real * src.real + src.imag * src.imag;

    CComplex ctmp((real * src.real + imag * src.imag) / tmp,
                 (real * src.real - imag * src.imag) / tmp);
    return ctmp;
}

//----------------------------- 重載== -------------------------
bool operator==(const CComplex & dest, const CComplex & src)
{
    return (dest.real == src.real && dest.imag == src.imag);
}

//----------------------------- 重載!= -------------------------
bool operator!=(const CComplex & dest, const CComplex & src)
{
    return !(dest.real == src.real && dest.imag == src.imag);
}

//----------------------------- 重載[] -------------------------
CComplex& CComplex::operator[](int index)
{
    assert(index >= 0 && index <= 100);

    return this[index];
}

//----------------------------- 重載<< -------------------------
ostream & operator<<(ostream &out, const CComplex & src)
{
    out << src.real;
    src.imag < 0 ? (out << "-") : (out << "+");
    out << src.imag << "i";

    return out;
}

//----------------------------- 重載>> -------------------------
istream & operator>>(istream &in, CComplex & src)
{
    in >> src.real >> src.imag;

    return in;
}

//前置自增
CComplex  CComplex::operator++()
{
    real ++;
    imag ++;

    return *this;
}

//後置自增
CComplex  CComplex::operator++(int )
{
    CComplex tmp(real, imag);
    real ++;
    imag ++;

    return tmp;
}

//前置自減
CComplex  CComplex::operator--()
{
    real --;
    imag --;

    return *this;
}

//後置自減
CComplex  CComplex::operator--(int x)
{
    CComplex tmp(real, imag);
    real --;
    imag --;

    return tmp;
}

//重載圓括號
CComplex CComplex::operator()(float r, float i)
{
    real = r;
    imag = i;

    return *this;
}

//重載圓括號
CComplex CComplex::operator()(float r)
{
    real = r;
    imag = 0;

    return *this;
}




#include <iostream>
using namespace std;
#include <conio.h>

#include "CComplex.h"

int main()
{
    CComplex c1(1, 1), c2(2, -1), c3, c4(c1);

    c3 = c1;
    cout << "c3 = c1:" << c3 << endl << endl;

    cout << "c4(c1) " << c4 << endl << endl;

    cout << "sizeof(CComplex): " << sizeof(CComplex) << endl << endl;
    cout << c1 + c2 << endl;
    cout << c1 - c2 << endl;
    cout << c1 * c2 << endl;
    cout << c1 / c2 << endl;
    cout << (c1 == c2) << endl;
    cout << (c1 != c2) << endl;

    cout << "c1: " << c1 << endl;
    cout << "++c1: " << ++c1 << endl;
    cout << "c1: " << c1 << endl << endl;

    cout << "c1++: " << c1++ << endl;
    cout << "c1: " << c1 << endl << endl;

    cout << "c1--: " << c1-- << endl;
    cout << "c1: " << c1 << endl << endl;

    cout << "--c1: " << --c1 << endl;
    cout << "c1: " << c1 << endl << endl;

    CComplex c5,c6;

    c5(4, 5);//測試圓括號運算符重載
    cout << "c5: " << c5 << endl;

    c6(6);
    cout << "c6: " << c6 << endl;


    cout << "輸入3個複數" << endl;
    CComplex c[3];
    for (int i = 0; i < 3; i++)
        cin >> c[i];

    cout << "這三個複數是:" << endl;
    for (int i = 0; i < 3; i++)
        cout << c[i] << endl;

    getch();
    return 0;
}


最後更新:2017-04-02 15:15:21

  上一篇:go 如何成為一個技術“牛人”
  下一篇:go 編程思維訓練(一)