运算符的重载(复数的相关运算)

运算符的重载实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该重载的运算符时调用此函数。这个函数叫做运算符重载函数,通常为类的成员函数。

     定义运算符重载函数的一般格式:

返回值类型 类名::operator重载的运算符(参数表)

{……}

operator是关键字,它与重载的运算符一起构成函数名。因函数名的特殊性,C++编译器可以将这类函数识别出来。

具体的加减乘除等代码运算如下:

#include<iostream>
#include<string>
using namespace std;

class Complex;                                       //提前声明
Complex operator+(int x, Complex &c);                //整数和对象的运算
ostream& operator<<(ostream &out, const Complex &c); //输出流
istream& operator>>(istream &in, Complex &c);        //输入流

class Complex                                        //定义Complex类
{
	friend istream& operator>>(istream &in, Complex &c);//定义友元,可以访问私有成员
	friend ostream& operator<<(ostream &out, const Complex &c);
	friend Complex operator+(int x, Complex &c);
public:
	Complex(const double real=0,const double imag=0):m_real(real),m_imag(imag)//构造函数(初始化)
	{}
	~Complex()                                       //析构函数
	{}
	void Show()const                                 //显示
	{
		cout<<"("<<m_real<<","<<m_imag<<")"<<endl;
	}
	Complex operator+(int x)                         //整型加
	{
		return Complex(m_real+x,m_imag);
	}
	Complex operator+(Complex &c)                    //对象加
	{
		return Complex(m_real+c.m_real,m_imag+c.m_imag);
	}
	Complex operator-(Complex &c)                    //对象减
	{
		return Complex(m_real-c.m_real,m_imag-c.m_imag);
	}
	Complex operator*(Complex &c)                    //对象乘
	{
		return Complex(m_real*c.m_real-m_imag*c.m_imag,
			   m_real*c.m_imag+m_imag*c.m_real);
	}
	Complex operator/(Complex &c)                    //对象除
	{
		double t = c.m_real*c.m_real+ c.m_imag * c.m_imag;//分母
		return Complex((m_real *c.m_real+ m_imag * c.m_imag)/t,
			   (m_imag *c.m_real - m_real * c.m_imag)/t);
	}
	Complex operator+=(Complex &c)                    //对象加等于
	{
		return Complex(m_real+m_real+c.m_real,m_imag+m_imag+c.m_imag);
	}
	Complex operator-=(Complex &c)                    //对象减等于
	{
		return Complex(c.m_real,c.m_imag);
	}
	Complex operator*=(Complex &c)                    //对象乘等于
	{
		return Complex(m_real*m_real*c.m_real-m_real*m_imag*
			c.m_imag-m_imag*m_real*c.m_imag-m_imag*m_imag*c.m_real ,
			m_real*m_real*c.m_imag+m_real*m_imag*c.m_real+
			m_imag*m_real*c.m_real-m_imag*m_imag*c.m_imag);
	}
	Complex operator/=(Complex &c)                    //对象除等于
	{
		double t = c.m_real*c.m_real+ c.m_imag * c.m_imag;//分母的分母
		double m = (((m_real *c.m_real+ m_imag * c.m_imag)/t)*//分母
			((m_real *c.m_real+ m_imag * c.m_imag)/t)
			+((m_imag *c.m_real - m_real * c.m_imag)/t)*
			((m_imag *c.m_real - m_real * c.m_imag)/t));
		double a = (m_real *c.m_real+ m_imag * c.m_imag)/t;//后值的实部
		double b = (m_imag *c.m_real - m_real * c.m_imag)/t;//后值的虚部
		return Complex((m_real*a-m_imag*b)/m,(m_imag*a+m_real*b)/m);
	}
private:
	double m_real;                                           //私有成员
	double m_imag;                                           //私有成员
};

Complex operator+(int x, Complex &c)                         //类外定义
{
	return Complex(x+c.m_real,c.m_imag);
}

ostream& operator<<(ostream &out, const Complex &c)
{
	out<<"("<<c.m_real<<","<<c.m_imag<<")";
	return out;
}

istream& operator>>(istream &in, Complex &c)
{
	in>>c.m_real>>c.m_imag;
	return in;
}

void main()                                                  //函数测试
{
	Complex c1(1,2);
	Complex c2(2,3);

	Complex n;
	int s;
	cin >> s;
	n = s;
	cout<<n<<endl;

	Complex a;
	a = c1 + c2;
	cout<<a<<endl;

	Complex b;
	b = c1 - c2;
	cout<<b<<endl;

	Complex c;
	c = c1 * c2;
	cout<<c<<endl;

	Complex d;
	d = c1 / c2;
	cout<<d<<endl;

	Complex e;
	e = c1 += c2;
	cout<<e<<endl;

	Complex f;
	f = c1 -= c2;
	cout<<f<<endl;

	Complex g;
	g = c1 *= c2;
	cout<<g<<endl;

	Complex k;
	k = c1 /= c2;
	cout<<k<<endl;

	Complex l;
	int x;
	cin >> x;
	l = x + c1;
	cout<<l<<endl;
}

如果有不完善的地方希望大家可以指出,谢谢~

时间: 2024-12-19 01:06:22

运算符的重载(复数的相关运算)的相关文章

结构体类型快速驱魔运算及运算符的重载

下面得到这段代码可以用在很多地方:只需要自己修改下接Ok. 1 struct Matrix 2 { 3 long long mat[N][N]; 4 Matrix operator*(const Matrix m)const//定义矩阵乘法的运算符* 5 { 6 Matrix tmp; 7 for(int i = 0;i < n;i++) 8 { 9 for(int j = 0;j < n;j++) 10 { 11 tmp.mat[i][j] = 0; 12 for(int k = 0;k &

第九周(运算符重载复数类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第九周(运算符重载) *作者:王忠 *完成日期:2015.5.13 *版本号:v1.0 * *问题描述:定义Complex类中的<<和>>运算符的重载,实现输入和输出, *输入描述: *程序输出: #include <iostream> using namespace std; class Complex { public: Complex(){rea

C++——运算符的重载---以成员函数方式重载---以友元函数方式重载

一.运算符的重载 1.运算符的重载 允许把标准运算符(如+ - * /等运算符)应用于自定义数据类型的对象,可以提高程序的可读性,运算符的重载本质上还是函数重载.运算符仅仅是语法上的方便,它是另一种函数调用的方式,只有在设计涉及的代码更容易写,尤其是更容易读的时候才有必要重载. 2.实现运算符重载的方式 类的成员函数 友元函数(即类外的普通函数) 3.运算符重载的原则: 不能重载的运算符有5个:  .  .*  ?: ::  sizeof 运算符重载不允许发明新的运算符 不能改变运算符操作对象的

C++ 运算符的重载

#include <iostream> #include <stdio.h> using namespace std; class Complex //复数类 { public: double real;//实数 double imag;//虚数 Complex(double real=0,double imag=0) { this->real=real; this->imag=imag; } }; Complex operator+(Complex com1,Comp

matlab矩阵合并及相关运算

1.matlab允许向量(和矩阵)合并,且matlab提供了两种合并方式,[a,b]和[a;b],两者的结果是不一样的. a=rand(2,3): b=rand(2,3): c=[a;b]: d=[a,b]: c的结果是将b整体合并到a 的下边,而d的结果是整体将b合并到a 的右边. 2.创建等差向量组 a=[1:2:11] 注意涉及到向量内部对应数据之间的运算时一定要用点运算符号,(.)例如,求表达式b=a^2时应该写作 b=a.^2 也可以利用linspace来创建等差向量,linspace

在复数类中自定义类型转换函数实现复数和非复数之间的运算

实现复数+double型数据,并且打印运算后实部上的数据 #include <iostream> using namespace std; class Complex { public: Complex( )//定义默认构造函数初始化复数 { real=0; imag=0; } //使用初始化表初始化复数 Complex(double r, double i):real(r),imag(i){} //定义自定义类型转换函数 operator double() { return real; }

深入理解运算符的重载

*运算符重载: 1.运算符重载就是赋予已有的运算符多重含义,即多种功能. 2.运算符重载的目的:通过运算符的重载即重新定义使得其能够用于特定类的对象执行特定的功能. 3.对于运算符的重载首先要关心的就是那些运算符能够重载,那些不能重载: 能够重载的运算符: (1).算术运算符:+,-,*,/,%,++,--: (2)位操作运算符:&,|,^,~,<<,>>: (3)逻辑运算符:!,&&,||: (4)比较运算符:>,<,>=,<=,=

js 时间函数 及相关运算大全

js 时间函数 及相关运算大全 var myDate = new Date(); myDate.getYear();        //获取当前年份(2位) myDate.getFullYear();    //获取完整的年份(4位,1970-????) myDate.getMonth();       //获取当前月份(0-11,0代表1月) myDate.getDate();        //获取当前日(1-31) myDate.getDay();         //获取当前星期X(0-

C++ 运算符的重载(转载自http://blog.csdn.net/insistgogo/article/details/6626952)

(转载自http://blog.csdn.net/insistgogo/article/details/6626952) 什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 或 友元函数(类外的普通函数) 规则:不能重载的运算符有 .  和 .* 和 ?: 和 ::  和 sizeof 友元函数和成员函数的使用场合:一般情况下,建议一元运算符使用成员函数,二元运算符使