如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部
2、用GetReal() GetImaginary()返回实部、虚部,均为double型
#include<iostream> using namespace std; class Cmycomplex { private: double real,imag; public: Cmycomplex(double r=0,double i=0);//这里可以给构造函数默认值,也可以重载构造函数 double GetReal(); double GetImaginary(); }; Cmycomplex::Cmycomplex(double r,double i) { real=r; imag=i; } double Cmycomplex::GetReal() { return real; } double Cmycomplex::GetImaginary() { return imag; }
//StudybarCommentBegin int main() { Cmycomplex z1(2,3),z2,z3(3); cout<<z1.GetReal()<<"\n"; cout<<z2.GetImaginary()<<"\n"; cout<<z3.GetReal()<<"\n"; } //StudybarCommentEnd
-END
标签:虚部,double,实部,复数,GetImaginary,Cmycomplex,构造函数 From: https://www.cnblogs.com/peitongshi/p/17340262.html