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;
}
时间: 2025-01-21 00:06:37

C++上机实验三:运算符重载的相关文章

C02-程序设计基础提高班(C++)第11周上机任务-运算符重载

第11周:阅读教材第10章(p314-346),掌握用运算符重载解决问题,完成第11周上机任务: (回到C02-程序设计基础提高班(C++)学习安排) [任务1]实现复数类中的运算符重载定义一个复数类重载运算符+.-.*./,使之能用于复数的加减乘除.(1)方案一:请用类的成员函数完成运算符的重载: class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Comple

c++-C++ list赋值和类的运算符重载

问题描述 C++ list赋值和类的运算符重载 刚刚发现了个问题,一直卡着我 class CA { public: CA(){} ~CA(){} public: VOID operator = ( CA& msg ) { dwvalue = msg.dwvalue; } private: DWORD dwvalue; }; VOID Fuck1( OUT list& bb ) { list< CA > aa; CA a; CA b; aa.push_back(a); aa.pu

关于C++加法运算符重载的一个超简单问题

问题描述 关于C++加法运算符重载的一个超简单问题 以下代码: #include using namespace std; class num { public: num(){n=1;cout<<"构造函数执行 ";} num(int i){n=i;cout<<"带一个参数的构造函数执行 ";} num(const num&s){this->n=s.n;cout<<"复制构造函数执行 ";} ~n

c++-求大神!关于IO 输出运算符重载 这是什么错误 怎么改?

问题描述 求大神!关于IO 输出运算符重载 这是什么错误 怎么改? #include"iostream" #include"fstream" #include"string" using namespace std; class coStu { protected: string name,num; double score; public: coStu(string na,string nu,double s): name(na),num(nu

多项式 运算符重载-利用运算符重载 定义多项式加法 返回值出错

问题描述 利用运算符重载 定义多项式加法 返回值出错 class Poly{ int n; public: int *a; Poly(){} Poly(int) { cout<<""请输入多项式的次数:""; cin>>n; a =new int [n+1]; cout<<""请依次输入各项系数 不存在该次项则输入0 最高项系数不得为0""< for(int i=0;i { cout

c++运算符重载冲突问题

问题描述 c++运算符重载冲突问题 在头文件中加入#include < memory >以后,很多运算符都提示错误了,比如cin>>n;里的>>这种. 去掉头memory头文件就都好了.怎么避免这个冲突? 解决方案 可能是编译器不支持memory吧,你用memory是要用来分配内存麽. 解决方案二: 我怎么没有遇到这种问题,你得把问题再描述清楚一点 解决方案三: 额,for循环里不应该用的是;分隔么? 要不试试 #include 解决方案四: 使用命名空间,指定定义域就

matrix-C++运算符重载实现连加

问题描述 C++运算符重载实现连加 这是函数原型,成员函数:Matrix operator+(const Matrix&) 这是函数体 Matrix Matrix::operator+(const Matrix& m) { Matrix temp; for (int i = 0;i < 2;i++) { for (int j = 0;j < 2;j++) { temp.p[i][j] = p[i][j] + m.p[i][j]; } } return temp; } 运行时出现

c语言-关于c运算符重载的问题

问题描述 关于c运算符重载的问题 最近看到一段代码 bool operator < (const xx&x) const{...} 这是将运算符重载, 请问参数表列后加了一个const是什么意思? 解决方案 两个const参数中的那个说明参数本身是常量,不能修改.后面那个表示,这个函数不会修改任何对象实例中的成员. 解决方案二: 举例 class A { int x; public: void foo() const { /* x = 1; */ } public: void bar() {

os-c++中运算符重载 这个小程序怎么不对呢

问题描述 c++中运算符重载 这个小程序怎么不对呢 #include using namespace std; class R{ public : int n; int d; R(int a,int b) { this->n=a; this->d=b; } }; ostream operator<< (ostream &os,R &r) { // os<<r.n<<endl; // os<<r.d<<endl;; os