⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1-1.cpp

📁 平常学习c++的一些小程序
💻 CPP
字号:
//Please carefully review what we have learned about operator overloading, 
//debug the programs from the PPT in the computer, and understand the mechanism of it.   

#include <iostream.h>
class complex
{
public:
    complex(double r=0,double i=0);
    complex operator +(const complex& c);
    complex operator -(const complex& c);
    complex operator -();
    void print() const;// const member function
private:
    double real,imag;
};
 complex::complex(double r,double i)
 :real(r),imag(i){}

 
complex complex::operator +(const complex& c)
{
    double r=real+c.real;
    double i=imag+c.imag;
    return complex(r,i);
}
complex complex::operator -(const complex& c)
{
    double r=real-c.real;
    double i=imag-c.imag;
    return complex(r,i);
}

complex complex::operator -()
{
    return complex(-real,-imag);
}
void complex::print() const
{
    cout<<"("<<real<<","<<imag<<")"<<endl;
}
void main()
{
    complex c1(2.5,3.7),c2(4.2,6.5);
    complex c;
    c=c1-c2;
    c.print();
    c=c1+c2;
    c.print();
    c=-c1;
    c.print();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -