首页 > 编程语言 > 详细

【c++】实现一个复数类

时间:2015-04-25 12:13:34      阅读:288      评论:0      收藏:0      [点我收藏+]
// 实现一个复数类(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;
}


技术分享


技术分享


技术分享


技术分享



【c++】实现一个复数类

原文:http://blog.csdn.net/zhaoyaqian552/article/details/45268895

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!