运算符重载

作者: 逍遥_9353 | 来源:发表于2018-05-17 01:35 被阅读107次

/*

定义复数类complex,包括私有数据成员实部real和虚部image。定义该类的构造,拷贝构造,析构函数。为该类重载运算符+,-(友元函数),前置和后置++,--(成员函数),插入符和提取符<<,>>(友元函数)。在main函数里定义复数对象,测试重载的这些运算符。

*/

#include<iostream>

#include<string>

using namespace std;

class Complex

{

public:

    Complex(int real1=0,int image1=0) :real(real1),image(image1)

{

}

// Complex(Complex & con_recomplex)

// {

// real=con_recomplex.real;

// image=con_recomplex.image;

//     cout<<"complex constructor one!"<<endl;

// }

~Complex()

{

};

    friend Complex operator+(const Complex &a1, const Complex &a2);

friend Complex operator-(const Complex &a1, const Complex &a2);

Complex operator++();

Complex operator++(int);

Complex operator--();

Complex operator--(int);

friend ostream& operator<<(ostream& os, const Complex&a3);

friend istream& operator>>(istream& is, Complex&a3);

private:

    int real;

int image;

};

Complex operator+(const Complex &a1, const Complex &a2)

{

    return Complex(a1.real + a2.real, a1.image + a2.image);

}

Complex operator-(const Complex &a1, const Complex &a2)

{

    return Complex(a1.real - a2.real, a1.image - a2.image);

}

Complex Complex::operator++()

{

    ++real;

++image;

return *this;

}

Complex Complex::operator++(int)

{

    Complex a = *this;

++(*this);

return a;

}

Complex Complex::operator--()

{

    --real;

--image;

return *this;

}

Complex Complex::operator--(int)

{

    Complex a = *this;

--(*this);

return *this;

}

ostream& operator<<(ostream& os, const Complex& a3)

{

    os<< a3.real << "+" << a3.image << "i";

return os;

}

istream& operator>>(istream& is, Complex&a3)

{

    is >> a3.real >> a3.image;

return is;

}

int main()

{

    Complex a4(4,5), a5(6,7),a6;

cout << "a4:" << a4 << endl;

cout << "a5:" << a5 << endl;

cin >> a4;cin >> a5;

cout << "重新输入后 a4:" << a4 << endl;

cout << "重新输入后 a5:" << a5 << endl;

a6 = a4 + a5;

    cout << a6 << endl;

a6 = a4 - a5;

cout << a6 << endl;

cout <<"++a4:"<<++a4 << endl;

cout <<"a5++:" <<a5++ << endl;

cout << "--a4:" << --a4 << endl;

cout <<"a5-:" << a5-- << endl;

return 0;

}                 

相关文章

  • 运算符重载及其他约定

    7.1 重载算术运算符 重载二元算术运算符 重载复合赋值运算符 重载一元运算符 重载比较运算符 等号运算符:‘eq...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • 2019-07-11 运算符的重载

    运算符的重载 重载运算符 (“::”,“.*”,“.”,“?:”)不能被重载。 重载运算符时,不能改变其本质,如不...

  • 运算符重载

    一.+号运算符重载 二.<<号运算符重载 三.指针运算符重载(智能指针)

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

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

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

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

  • C++重载

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

  • C++运算符重载详解

    运算符重载规则 1.被重载的运算符必须是已经存在的C++运算符,不能重载自己创建的运算符; 2.运算符被重载之后,...

  • 第十一章 使用类

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

  • 4.0 C++远征:重载运算符

    重载运算符 [TOC] 四、重载运算符 ​ 概念 : 给原有运算符赋予新功能。 ​ 本质 : 函数重载。 ...

网友评论

    本文标题:运算符重载

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