首页 > 编程语言 > 详细

初识c++,复数类

时间:2015-11-10 16:23:15      阅读:214      评论:0      收藏:0      [点我收藏+]
//#include<iostream>
//using namespace std;
//class  Oxen{
//  public:
//	  Oxen(double  real = 1.0, double imaginary = 1.0)
//	  {
//		  _real = real;
//		  _imaginary = imaginary;
//	  }
//	  void  Display();
//	  Oxen  Add(Oxen &a, Oxen &b);
//  //private:
//	  double  _real;
//	  double  _imaginary;
//};
//void Oxen::Display()
//{
//	printf("%d+%di\n", _real, _imaginary);
//}
//Oxen  Add(Oxen &a, Oxen &b)
//{
//	Oxen  ret;
//	ret._real = a._real + b._real;
//	ret._imaginary = a._imaginary + b._imaginary;
//	return  ret;
//}
//int  main()
//{
//	Oxen  value(2);
//	Oxen  val(2, 3);
//	Oxen  ret = Add(value, val);
//	value.Display();
//	ret.Display();
//	system("pause");
//	return 0;
//}
#include<iostream>
using  namespace std;
class Complex
{
	// 构造函数(无参、带参、带缺省值、半缺省值的构造函数都去练练 )
	// 析构函数
	// 拷贝构造函数
	// 赋值运算符重载
	public:
		/*Complex()
		{
			_real = 1.0;
			_image = 1.0;
			cout << "构造函数" << endl;
		}
		Complex(double real, double  image)
		{
			_real = real;
			_image = image;
		}
		Complex(double  real, double image = 1.0)
		{
			_real = real;
			_image = image;
		}*/
		Complex(double  real = 1.0, double image = 1.0)
		{
			_real = real;
			_image = image;
		}
		
		Complex(Complex& a)
		{
			_image = a._image;
			_real = a._real;
		//	cout << "复制构造" << endl;
		}
		
		~Complex()
		{
			//cout << "析构函数" << endl;
		}
		Complex& operator ++();    // 前置 ++
		Complex operator ++(int);  // 后置++
		Complex& operator --();   // 前置 -
		Complex operator --(int); // 后置-
		Complex operator +(const Complex& c);
		Complex operator -(const Complex& c);
		Complex& operator -=(const Complex& c);
		Complex& operator +=(const Complex& c);
		Complex operator *(const Complex& c);
		Complex operator /(const Complex& c);
		void  Display();
	private:
		double  _real;
		double  _image;
	};
Complex& Complex:: operator++()
{
	++this->_real;
	++this->_image;
	return *this;
}
Complex  Complex::operator++(int)//int是个标记表示后置++,只作为函数重载特征
{
	Complex tmp;
	tmp._real = this->_real++;
	tmp._image = this->_image++;
	return  tmp;
}
Complex&  Complex:: operator--()
{
	--this->_real;
	--this->_image;
	return *this;
}
Complex  Complex::operator--(int)//int是个标记表示后置--,只作为函数重载特征
{
	Complex tmp;
	tmp._real = this->_real--;
	tmp._image = this->_image--;
	return  tmp;
}
Complex  Complex::operator+(const Complex &c)
{
	this->_real += c._real;
	this->_image += c._image;
	return *this;
}
Complex  Complex::operator-(const Complex &c)
{
	this->_real -= c._real;
	this->_image -= c._image;
	return *this;
}

Complex &   Complex::operator+=(const Complex & c)
{
	this->_real += c._real;
	this->_image += c._image;
	return *this;
}
Complex &  Complex:: operator-=(const Complex & c)
{
	this->_real -= c._real;
	this->_image -= c._image;
	return *this;
}

Complex  Complex:: operator*(const Complex &c)
{
	//(a+bi)(c+di)=(ac-bd)+(bc+ad)i;
	Complex tmp;
	tmp._real = (this->_real*c._real) - (this->_image*c._image);
	tmp._image = (this->_image*c._real) + (this->_real*c._image);
	return tmp;
}
Complex  Complex:: operator/(const Complex &c)
{
	//(a+bi)/(c+di)=x+yi,x=(ac+bd)/(c*c+d*d),y=(bc-ad)/(c*c+d*d)
	Complex tmp;
	tmp._real = ((this->_real*c._real) + (this->_image*c._image)) / (c._real*c._real + c._image*c._image);
	tmp._image = ((this->_image*c._real) - (this->_real*c._image)) / (c._real*c._real + c._image*c._image);
	return tmp;
}
void  Complex::Display()
{
	cout << "real:" << _real;
	cout <<" image"<< _image << endl;
}

void  Test1()
{
	Complex a, b;
	a.Display();
	Complex c=++a;//前置调用
	c.Display();
	Complex d;
	d=b++;
	d.Display();
	a=a + b;
	a.Display();
	//a + (b,9);//逗号表达式
	

}
void Test2()
{
	Complex a(1, 2);
	Complex b(3, 4);
	/*Complex c = a*b;
	c.Display();*/
	a -= b;
	a.Display();
}
int  main()
{
	Test2();
	system("pause");
	return 0;
}


本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1711381

初识c++,复数类

原文:http://10541556.blog.51cto.com/10531556/1711381

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