第8周项目1(3)—扩充运算符功能
来源:互联网 发布:基于51单片机的交通灯 编辑:程序博客网 时间:2024/05/17 07:14
#include <iostream>using namespace std;class Complex{public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r; imag=i;} friend Complex operator+(const Complex &c1,const Complex &c2); friend Complex operator+(const Complex &c1,double a); friend Complex operator-(const Complex &c1,const Complex &c2); friend Complex operator-(const Complex &c1,double a); friend Complex operator*(const Complex &c1,const Complex &c2); friend Complex operator*(const Complex &c1,double a); friend Complex operator/(const Complex &c1,const Complex &c2); friend Complex operator/(const Complex &c1,double a); void display();private: double real; double imag;};//下面定义成员函数Complex operator+(const Complex &c1,double a){ return Complex(c1.real+a,c1.imag);}Complex operator-(const Complex &c1,double a){ return Complex(c1.real-a,c1.imag);}Complex operator*(const Complex &c1,double a){ return Complex(c1.real*a,c1.imag*a);}Complex operator/(const Complex &c1,double a){ return Complex(c1.real/a,c1.imag/a);}Complex operator+(const Complex &c1,const Complex &c2){ return Complex(c1.real+c2.real,c1.imag+c2.imag);}Complex operator-(const Complex &c1,const Complex &c2){ return Complex(c1.real-c2.real,c1.imag-c2.imag);}Complex operator*(const Complex &c1,const Complex &c2){ return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c2.real*c1.imag);}Complex operator/(const Complex &c1,const Complex &c2){ return Complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(-c1.real*c2.imag+c2.real*c1.imag)/(c2.real*c2.real+c2.imag*c2.imag));}void Complex::display(){ if(imag>0) cout<<real<<"+"<<imag<<"i"<<endl; else cout<<real<<imag<<"i"<<endl;}//下面定义用于测试的main()函数int main(){ Complex c1(3,4),c2(5,-10),c3; double a=3; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); c3=c1+a; cout<<"c1+a="; c3.display(); c3=c1-a; cout<<"c1-a="; c3.display(); c3=c1*a; cout<<"c1*a="; c3.display(); c3=c1/a; cout<<"c1/a="; c3.display(); return 0;}
0 0
- 第8周项目1(3)—扩充运算符功能
- 第八周 项目1-扩充运算符功能
- 第八周 项目一(3):实现复数类中的运算符重载(扩充类)
- 【项目3 - 时间类】阅读、运行程序后,按要求扩充类的功能 第2周项目
- C++第8周项目5 - 扩充String类
- C++第8周(春)项目4 扩充String类
- 第8周项目四扩充string 类
- 第7周项目2-实现复数类中的运算符重载(3.完善功能)
- 第7周-项目1-完整实现复数类中的运算符重载-扩展+、-、*、/运算符的功能
- 第8周项目1-实现复数类中的运算符重载(3)
- 第8周项目1(3)实现复数类中的运算符重载(拓展)
- 第12周项目1—实现复数类中的运算符重载 (3)
- 第13周项目1—分数类中的运算符重载 (3)
- 第8周项目3分数运算符重载
- 第8周项目1运算符重载
- 第12周项目一(1)运算符重载
- 第八周 项目1-3 扩展重载运算符的功能(进化版)
- C++第8周项目1扩展 - 运算符重载进行虚数运算
- 第八周项目一 2用友元函数实现复数类中的运算符重载
- NGUI优化方法
- android菜单(menu)资源
- 文章标题
- @Resource & @Autowired
- 第8周项目1(3)—扩充运算符功能
- 第八周项目1——复数类中的运算符重载(3)
- PTO Paid Time Off 带薪休假
- centos7 安装wmware workstation 11
- ios 编译libx264,libfaac
- VBA IP相关的转换程序
- linux下dd命令使用方法
- qt把自定义类输入到二进制文件中,从二进制文件中读取类
- 分数类的运算符重载(1)