课堂小练习: 设计、定义并实现Complex类
#include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex(double r=0.0,double i=0.0); Complex(float r2=0.0); Complex(Complex &c); void add(Complex c2 ); void show(); double mod(); private: double real,imaginary; }; Complex::Complex(double r,double i):real(r),imaginary(i){ } Complex::Complex(Complex &c):real(c.real),imaginary(c.imaginary){ } Complex::Complex(float r2):real(r2),imaginary(0){ } void Complex::add(Complex c2){ real=real+c2.real; imaginary=imaginary+c2.imaginary; } void Complex::show(){ cout<<real<<"+"<<imaginary<<"i"<<endl; } double Complex::mod(){ return sqrt(pow(real,2)+pow(imaginary,2)); } int main(){ Complex c1(3,5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }
总结:
1:在定义Complex(float r2=0.0);时 我用double就表明有错误,有点不明白????
2:感觉框架不熟悉。
评论:
https://www.cnblogs.com/csl-40/p/10625358.html
https://www.cnblogs.com/sqcmxg/p/10628801.html
https://www.cnblogs.com/qiuqiuwr/p/10626865.html