Complex类
作者:互联网
#include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double a=0,double b=0); Complex(Complex &p); void show(); void add(Complex c); double mod(); private: double real; double imaginary; }; Complex::Complex(double a,double b){ real=a; imaginary=b; } Complex::Complex(Complex &p) { real=p.real; imaginary=p.imaginary; } void Complex::add(Complex c) { real=real+c.real; imaginary=imaginary+c.imaginary; } void Complex::show() { cout<<real<<"+"<<imaginary<<"i"<<endl; } double Complex::mod() { double n; n=sqrt(real*real+imaginary*imaginary); return n; } int main() { Complex c1(3,5); cout<<"c1="; c1.show(); cout<<"|c1|="<<c1.mod()<<endl; Complex c2(4.5); cout<<"c2="; c2.show(); cout<<"c1+c2="; c1.add(c2); c1.show(); return 0; }
实验总结与体会:本次实验让我更加了解Complex类,实验过程总计一个多小时,因为对复制构造函数掌握的不好,还去书上查找了例题,总之是有收获的一次实验。
标签:real,double,void,Complex,实验,imaginary 来源: https://www.cnblogs.com/mzx1999/p/10631597.html