小练习
作者:互联网
基于习题4-20要求,设计并实现Complex类:
#include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(float real=0,float imaginary=0); void add(Complex c); void show(); double mod(); private: float r,i; }; Complex::Complex(float real,float imaginary){ r=real; i=imaginary; } void Complex::add(Complex c){ r+=c.r; i+=c.i; } void Complex::show(){ cout<<r<<"+"<<i<<"i"<<endl; } double Complex::mod(){ double m; m=sqrt(r*r+i*i); return m; } int main(){ Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }View Code
我寻思着,大家咋都有个9.01388,原来还有个求模啊
标签:real,练习,show,void,float,Complex,imaginary 来源: https://www.cnblogs.com/kori/p/10633012.html