// 实现一个复数类(complex) //要求:1:实现不带参 / 带参数 /带默认参数的几种构造函数。 // 2:实现拷贝构造 / 析构函数。 #include <iostream> using namespace std; // 带参数的构造函数 class complex { private: double _real; double _image; public: void print(); complex(double _r, double _i); ~complex(); }; complex::complex(double _r, double _i) { _real = _r; _image = _i; cout<<"the parameterless constructor"<<endl; } /* 不带参数的构造函数 class complex { private: double _real; double _image; public: void print(); complex(); ~complex(); }; complex::complex() { _real = 0; _image = 0; cout<<"with no arguments constructor"<<endl; }*/ /* 带默认参数的构造函数 class complex { private: double _real; double _image; public: void print(); complex(double _real = 0,double _image = 0); ~complex(); }; complex::complex(double _r, double _i) { _real = _r; _image = _i; cout<<"with the default constructor"<<endl; }*/ /* 拷贝构造 class complex { private: double _real; double _image; public: void print(); complex(double _r, double _i) { _real = _r; _image = _i; } complex(complex const & comple); ~complex(); }; complex::complex(complex const & comple) { _real = comple._real; _image = comple._image; cout<<"copy structure"<<endl; }*/ complex::~complex() { cout << "destructor" << endl; } void complex::print() { cout << "复数是:" << _real << "+" <<_image << "i" << endl; } int main() { double a, b; //double a; cout << "请输入两个数:"; cin >> a >> b; //cin >> a; //complex comp(a); complex comp(a,b); //complex comp(1, 2); //complex comp; //complex comp1(comp); comp.print(); //comp1.print(); return 0; }
原文:http://blog.csdn.net/zhaoyaqian552/article/details/45268895