[C ++] Operator overloading of the plural and Operator Overloading

Source: Internet
Author: User

[C ++] Operator overloading of the plural and Operator Overloading



Definition of operator overloading:


You can perform operations on custom types, such as complex operations. You need to redefine the operator number (create a function ).

Except for the class Relational operators ".", member pointer operators ". *", scope operators ":", sizeof operators, and three-object operators "? : ", All operators in C ++ can be overloaded.




Complex operators are overloaded as follows:

<span style="font-size:18px;">
#include<iostream>#include<string>using namespace std;class Complex;Complex operator+(int x, Complex &c);ostream& operator<<(ostream &out, const Complex &c);istream& operator>>(istream &in, Complex &c);class Complex{friend istream& operator>>(istream &in, Complex &c);friend ostream& operator<<(ostream &out, const Complex &c);friend Complex operator+(int x, Complex &c);public:Complex(const int real = 0, const int imag = 0) :m_real(real), m_imag(imag){}~Complex(){}void Show()const{cout << "(" << m_real << "," << m_imag << ")" << endl;}public:Complex operator+(int x){return Complex(m_real + x, m_imag);}Complex operator+(Complex &c){return Complex(m_real + c.m_real, m_imag + c.m_imag);}Complex operator-(Complex &c){return Complex(m_real - c.m_real, m_imag - c.m_imag);}friend Complex operator+(int x, Complex &c){return Complex(x + c.m_real, c.m_imag);}friend Complex operator-(int x, Complex &c){return Complex(x - c.m_real, -c.m_imag);}Complex operator*(Complex &c);friend Complex operator*(int x, Complex &c);Complex operator/(Complex &c);friend Complex operator/(int x, Complex &c);Complex operator+=(Complex &c);Complex operator-=(Complex &c);Complex operator*=(Complex &c);Complex operator/=(Complex &c);private:int m_real;int m_imag;};Complex Complex::operator/(Complex &c){Complex temp;temp.m_real = (m_real*c.m_real + m_imag*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);temp.m_imag = (m_imag*c.m_real - m_real*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);return temp;}Complex operator/(int x, Complex &c){Complex temp;temp.m_real = (x*c.m_real) / (c.m_real*c.m_real + c.m_imag*c.m_imag);temp.m_imag = (-x*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);return temp;}Complex Complex:: operator+=(Complex &c){m_real = m_real + c.m_real;m_imag = m_imag + c.m_imag;return *this;}Complex Complex::operator-=(Complex &c){m_real = m_real - c.m_real;m_imag = m_imag - c.m_imag;return *this;}Complex Complex:: operator*(Complex &c){return Complex(m_real*c.m_real - m_imag*c.m_imag, m_imag*c.m_real + m_real*c.m_imag);}Complex operator*(int x, Complex &c){return Complex(c.m_real*x, c.m_imag*x);}Complex Complex::operator*=(Complex &c){int i = m_real;m_real = m_real*c.m_real - m_imag*c.m_imag;m_imag = m_imag*c.m_real + i*c.m_imag;return *this;}Complex Complex:: operator/=(Complex &c){m_real = (m_real*c.m_real + m_imag*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);m_imag = (m_imag*c.m_real - m_real*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);return *this;}ostream& operator<<(ostream &out, const Complex &c){out << "(" << c.m_real << "," << c.m_imag << ")";return out;}istream& operator>>(istream &in, Complex &c){in >> c.m_real >> c.m_imag;return in;}int main(){Complex c1(2, 1);Complex c2(3, 2);Complex c;c = c1 + c2;cout<<c1<<"+"<<c2<<"="<<c<<endl;c = c1 * c2;cout << c1 << "*" << c2 << "=" << c << endl;c = c1 / c2;cout << c1 << "*" << c2 << "=" << c << endl;c = 2 + c1;cout <<"2"<< "+" << c1 << "=" << c << endl;//    c1+=c2;//    cout<<c1<<endl;//    c1-=c2;//    cout<<c1<<endl;c1 *= c2;cout << c1 << endl;c1 /= c2;cout << c1 << endl;c = 4 / c1;cout << c << endl; Complex c3; cin>>c3; cout<<c3;return 0;}
</span>



Running result:






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.