C++运算符重载

来源:互联网 发布:网络对青少年的危害 编辑:程序博客网 时间:2024/06/06 19:26

IO运算符重载

#include <bits/stdc++.h>using namespace std;class Point{private:    double m_dx, m_dy, m_dz;public:    friend ostream& operator << (ostream& out, Point& cPoint);    friend istream& operator >> (istream& in, Point& cPoint);    Point(double dx=0.0, double dy=0.0, double dz=0.0)    {        this->m_dx=dx;        this->m_dy=dy;        this->m_dz=dz;    }    void Setx(double a)    {        this->m_dx = a;    }    void Sety(double b)    {        this->m_dy = b;    }    void Setz(double c)    {        this->m_dz = c;    }    double GetAddXYZ(double &px)    {        px = this->m_dz+this->m_dx+this->m_dy;        return px;    }};//--------------重载I/0运算符//重载运算符 <<ostream& operator << (ostream& out, Point& cPoint){    cout << "(" << cPoint.m_dx << "," << cPoint.m_dy << "," << cPoint.m_dz << ")" << endl;    return out;}//重载运算符 >>istream& operator >> (istream& in, Point& cPoint){    in >> cPoint.m_dx;    in >> cPoint.m_dy;    in >> cPoint.m_dz;    return in;}int main(){    Point p;    //通过类来修改private成员变量.    p.Setx(1.1);    p.Sety(2.2);    p.Setz(3.3);    double result;    cout << p << endl;    // >>    cin >> p;    cout << p << endl;    return 0;}

这里写图片描述

算术运算符重载

#include <bits/stdc++.h>using namespace std;class Cent{public:    int m_nCents;    friend Cent operator + (const Cent& c1, const Cent& c2);    friend Cent operator - (const Cent& c1, const Cent& c2);    friend Cent operator * (const Cent& c1, const Cent& c2);    friend Cent operator / (const Cent& c1, const Cent& c2);    Cent(int mCents){this->m_nCents=mCents;}    int GetCents(){return m_nCents;}};Cent operator + (const Cent& c1, const Cent& c2){    return Cent(c1.m_nCents+c2.m_nCents);}Cent operator - (const Cent& c1, const Cent& c2){    return Cent(c1.m_nCents-c2.m_nCents);}Cent operator * (const Cent& c1, const Cent& c2){    return Cent(c1.m_nCents*c2.m_nCents);}Cent operator / (const Cent& c1, const Cent& c2){    return Cent(c1.m_nCents/c2.m_nCents);}int main(){    Cent c1(5), c2(2);    Cent c3(0);    c3 = c1 + c2;    cout << c3.m_nCents << endl;    Cent c4(0);    c4 = c1 - c2;    cout << c4.m_nCents << endl;    Cent c5(0);    c5 = c1 * c2;    cout << c5.m_nCents << endl;    Cent c6(0);    c6 = c1 / c2;    cout << c6.m_nCents << endl;    return 0;}

这里写图片描述

++,–运算符重载

#include <bits/stdc++.h>using namespace std;class Digit{public:    int m_nDigit = 0;    Digit(int n){this->m_nDigit = n;}    Digit& operator ++();    Digit& operator --();    //后缀    Digit& operator ++(int);    Digit& operator --(int);    int GetDigit() const{return m_nDigit;}};//前缀++,--Digit& Digit::operator++(){    ++this->m_nDigit;    return *this;}Digit& Digit::operator--(){    --this->m_nDigit;    return *this;}//后缀先赋值再运算Digit& Digit::operator++(int){    Digit cResult(this->m_nDigit);    ++(*this);    return cResult;}Digit& Digit::operator--(int){    Digit cResult(this->m_nDigit);    --(*this);    return cResult;}int main(){    Digit d1(9),d2(8);    d1--,d2++;    cout << d1.m_nDigit << " " << d2.m_nDigit << endl;    return 0;}

= 运算符重载

#include <bits/stdc++.h>using namespace std;class Cent{private:    int m_nCent;public:    Cent(int nCent){this->m_nCent=nCent;}    //拷贝构造函数    Cent(const Cent& nCent){this->m_nCent=nCent.m_nCent;}    //重载赋值运算符    Cent& operator=(const Cent& cent);    int GetCent()const{return this->m_nCent;}};Cent& Cent::operator=(const Cent& cCent){    if (this==&cCent)        return *this;    this->m_nCent=cCent.m_nCent;    return *this;}int main(){    Cent c1(1);    Cent c2 = c1;    cout << "After copy c1 to c2 :" << c2.GetCent() << endl;    Cent c3(4);    c3 = c1;    cout << "After c3 = c1 :" << c3.GetCent() << endl;    return 0;}

这里写图片描述

0 0