课堂小练习: 设计定义并实现Complex类
Posted jackyayue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课堂小练习: 设计定义并实现Complex类相关的知识,希望对你有一定的参考价值。
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double real1 = 0, double imaginary1= 0); Complex(Complex &p); void add(Complex t); void show(); double mod(); private: double real, imaginary; }; Complex::Complex(double real1, double imaginary1) { real= real1; imaginary= imaginary1; } Complex::Complex(Complex &p) { real=p.real; imaginary=p.imaginary; } void Complex::add(Complex t) { real=t.real+real; imaginary=t.imaginary+imaginary; } void Complex::show() { cout << real << "+" << imaginary<< "i" << endl; } double Complex::mod() { double a; a=sqrt(real*real+imaginary*imaginary); return a; } int main() { Complex c1(3, 5); Complex c2(4.5); Complex c3(c1); c1.add(c2); c1.show(); cout<<c1.mod(); return 0; }
总结:运用类的定义,这个框架运用还是不太熟悉。
以上是关于课堂小练习: 设计定义并实现Complex类的主要内容,如果未能解决你的问题,请参考以下文章