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;
}


目录
相关文章
|
4月前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
163 5
|
6月前
|
C++
C++(十五) 运算符重载
C++中的运算符重载允许对已有运算符的功能进行重新定义,从而扩展语言功能、简化代码并提升效率。重载遵循特定语法,如 `friend 类名 operator 运算符(参数)`。重载时需注意不可新增或改变运算符数量、语义、优先级、结合性和返回类型。常见示例包括双目运算符 `+=` 和单目运算符 `-` 及 `++`。输入输出流运算符 `&lt;&lt;` 和 `&gt;&gt;` 也可重载。部分运算符只能作为成员函数重载。
|
9月前
|
存储 编译器 C++
【C++】:拷贝构造函数和赋值运算符重载
【C++】:拷贝构造函数和赋值运算符重载
43 1
|
9月前
|
C++ 索引
C++核心技术要点《运算符重载》
C++核心技术要点《运算符重载》
66 2
|
8月前
|
自然语言处理 程序员 C++
C++基础知识(五:运算符重载)
运算符重载是C++中的一项强大特性,它允许程序员为自定义类型(如类或结构体)重新定义标准运算符的行为,使得这些运算符能够适用于自定义类型的操作。这样做可以增强代码的可读性和表达力,使得代码更接近自然语言,同时保持了面向对象编程的封装性。
|
8月前
|
Java 程序员 C++
|
8月前
|
编译器 C++
【C++】详解运算符重载,赋值运算符重载,++运算符重载
【C++】详解运算符重载,赋值运算符重载,++运算符重载
|
9月前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
9月前
|
存储 JavaScript 前端开发
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
|
9月前
|
C++
c++进阶篇(一)——运算符重载
c++进阶篇(一)——运算符重载