通过运算符重载实现复数运算

#include<iostream>
using namespace std;
class Complex
{
public:
    // 带缺省值的构造函数
    Complex(double real = 0, double image = 0)
    {
        cout << "带缺省值的构造函数" << endl;
        _real = real;
        _image = image;
    }
    // 析构函数
    ~Complex()
    {
        cout << "析构函数" << endl;
    }
    // 拷贝构造函数
    Complex(const Complex& d)
    {
        cout << "拷贝构造函数" << endl;
        _real = d._real;
        _image = d._image;
    }
    // 赋值运算符重载
    Complex& operator= (const Complex& d)
    {
        cout << "赋值运算符重载" << endl;
        if (&d != this)
        {
            _real = d._real;
            _image = d._image;
        }

        return *this;
    }
    void Display()
    {
        cout << "real = " << _real << "  " << "image = " << _image << endl;
    }

public:
    Complex& operator++()//前置++
    {
        _real++;
        return *this;
    }
    Complex operator++(int)//后置++ int为标示符区分前置++和后置++
    {
        Complex tmp(*this);
        _real++;
        return tmp;
    }

    Complex& operator--()//前置--
    {
        _real--;
        return *this;
    }
    Complex operator--(int)//后置--
    {
        Complex tmp(*this);
        _real--;
        return tmp;
    }

    Complex operator+(const Complex& c)
    {
        Complex tmp;
        tmp._real = _real + c._real;
        tmp._image = _image + c._image;
        return tmp;
    }
    Complex operator-(const Complex& c)
    {
        Complex tmp;
        tmp._real = this->_real - c._real;
        tmp._image = this->_image - c._image;
        return tmp;
    }

    Complex& operator-=(const Complex& c)
    {
        _real -= c._real;
        _image -= c._image;
        return *this;
    }
    Complex& operator+=(const Complex& c)
    {
        _real += c._real;
        _image += c._image;
        return *this;
    }

    Complex operator*(const Complex& c)
    {
        Complex tmp;
        tmp._real = (_real * c._real) - (_image * c._image);
        tmp._image = (_real * c._image) + (_image * c._real);
        return tmp;
    }
    Complex operator/(const Complex& c)
    {
        Complex tmp;
        double t = (c._real * c._real) + (c._image * c._image);
        tmp._real = (_real * c._real - _image * c._image) / t;
        tmp._image = (_real * c._image + _image * c._real) / t;
        return tmp;
    }

private:
    double _real;       // 实部
    double _image;      // 虚部
};
void TestMultiply()
{
    Complex c1(3.3, 1.1), c2(1.1, 2.2);
    Complex c3;
    c3 = c1 * c2; //(1.21,8.47)
    c3.Display();
}
void TestDivide()
{
    Complex c1(3.3, 1.1), c2(1.1, 2.2);
    Complex c3;
    c3 = c1 / c2; //(0.2,1.4)
    c3.Display();
}
void TestAdd_Reduce()
{
    Complex c1(4.3, 2.1), c2(1.5, 6.5), c3;
    c3 = c2 + c1;
    c3.Display();//(5.8,8.6)
    c3 = c2 - c1;//(2.2,1.1)
    c3.Display();
}

int main()
{
    Complex c1(2.2, 1.1);
    Complex c2(c1);//代入法拷贝构造
    c1.Display();
    c2.Display();
    Complex c3 = c1;//赋值法拷贝构造
    c3.Display();
    Complex c4(3.3, 1.1);
    c1 = c4;//赋值运算符重载
    c1.Display();

    ++c4;//(4.3,1.1)
    c4.Display();
    --c4;//(3.3,1.1)
    c4.Display();
    c3 = c4++;
    c3.Display();//(3.3,1.1)
    c4.Display();//(4.3,1.1)
    c4 -= c1;// (1,0)
    c4.Display();
    c4 += c1;//(4.3,1.1)
    c4.Display();

    TestAdd_Reduce();
    TestMultiply();
    TestDivide();
    getchar();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 03:26:09

通过运算符重载实现复数运算的相关文章

C++ 用运算符重载 实现复数相加

#include "stdafx.h" #include <iostream> using namespace std; class Complex {public: Complex(){real = 0;imag = 0;} Complex (double r, double i){real = r ;imag = i;} Complex operator+(Complex &c2); void display(); private: double real; d

C++ 关于运算符重载

转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下:    函数类型 operator 运算符名称 (形参表列)    {        // 对运算符的重载处理    } 例如,想将”+”用于Complex类(复数)的加法运算,函数的原型可以是这样的:    Complex operator+ (Complex& c1, Complex& c2);在上面的一般格式中,operator是关键字,是专门

C++基础5 运算符重载【提高】

1)括号运算符()重载 2)[面试题]&&, || 能不能做 操作符重载? 3)运算符极致练习: [提高]运算符重载 括号运算符()重载 [email protected]:~/c++$ cat main.cpp  #include <iostream> using namespace std; class A { public: A(int a,int b) { this->a = a; this->b = b; } int operator()(int a,in

C++:友元运算符重载函数

运算符重载函数:实现对象之间进行算数运算,(实际上是对象的属性之间做运算),包括+(加号).-(减号).*./.=.++.--.-(负号).+(正号) 运算符重载函数分为:友元运算符重载函数.成员运算符重载函数 运算符运算符重载函数按运算类型为:双目运算符重载函数,如加.减.乘.除.赋值:   单目运算符重载函数:自加.自减.取正负号 切记:成员运算符. 和->,sezeof等不能重载.运算符重载函数的参数至少有一个是类类型或引用类型, 下面为友元运算符重载函数举例: 1 #include<i

C++ Complex + - += -+ &lt;&lt;运算符重载

/* 提供了加减运算符重载的复数类头文件.关于传递引用和传递值,使用const 和不使用const,关于指针成员的使用需要继续学习...同理可以写出乘除法,共轭复数,绝对值, 等其他运算符重载. */ #ifndef _COMPLEX_H_#define _COMPLEX_H_#include<iostream>using namespace std;class Complex{public: //构造函数 Complex(double r = 0, double i = 0):re(r),

复数类的相关运算(判断大小及四则运算)-&gt;(构造,析构,拷贝复制,运算符重载)

问题描述: 创建一个Plural(复数)的class类,不借助系统的默认成员函数,在类体中写入构造函数,析构函数,拷贝复制函数以及运算符重载函数.并依次实现复数的大小比较(bool)和复数的四则运算(+,-,*,/). #include<iostream> using  namespace std; class Plural { public: void  Display() { cout << "Real->:" << _real; cout

C++复数运算符重载(+与&lt;&lt;)

Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算与输出操作.(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意.例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数).(2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i).(7.45-3.40i).(-3.25+4.13i)等.编写程序,分别求两个复数之和.整数和复数之和,并且输出. 请在下面的程序段基础上完

【C++】复数的运算符重载

运算符重载的定义: 用户对于自定义类型的运算操作,例如复数的运算.需要重新定义运算符号(创建函数). 除了类属关系运算符".".成员指针运算符".*".作用域运算符"::".sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载. 复数的运算符重载如下: <span style="font-size:18px;"> #include<iostream> #incl

sdut 4-1 复数类的运算符重载

4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描述 通过本题目的练习可以掌握成员运算符重载及友元运算符重载 要求定义一个复数类,重载加法和减法运算符以适应对复数运算的要求,重载插入运算符(<<)以方便输出一个复数的要求. 输入 要求在主函数中创建对象时初始化对象的值. 输出 输出数据共有4行,分别代表a,b的值和它们求和.求差后的值 示例输入 无 示例输出 a=3.2+4.5i b=8.9+5.6i a+b=12.1+10.1i