运算符重载问题

1.写出复数的所有运算符加减乘除,加加,减减的重载

#include
using namespace std;
class Complex
{
private:
	double real;
	double imag;
public:
	Complex(){real=0;imag=0;}//无参构造函数初始化
	Complex(double r,double i){real=r;imag=i;}//构造函数重载
	Complex operator +(Complex &c2)//对"+"号的重载
	{
		Complex c;
		c.real=real+c2.real;
		c.imag=imag+c2.imag;
		return c;
	}
	Complex operator -(Complex &c2)//对"-"号的重载
	{
		Complex c;
		c.real=real-c2.real;
		c.imag=imag-c2.imag;
		return c;
	}
	Complex operator *(Complex &c2)//对"*"号的重载
	{
		Complex c;
		c.real=real*c2.real-imag*c2.imag;
		c.imag=real*c2.imag+imag*c2.real;
		return c;
	}      
	Complex operator /(Complex &c2)//对"/"号的重载
	{
		Complex c;
		c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
		c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
		return c;
	} 
	void display();//定义输出函数
	void set();
};
void Complex::display()
{
	cout<<"("<input real=1
 imge=2
input real=2
 imge=2
c1=(1,2i)
c2=(2,2i)
c1+c2=(3,4i)
c1-c2=(-1,0i)
c1*c2=(-2,6i)

c1/c2=(0.75,0.25i)

2.编一个程序,用成员函数重载运算符“+”和“-”将两个二维数组相加和相减。

#include  
using namespace std;
const int row=2; 
const int col=3;
class array 
{  
public:
	array()
	{
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				var[i][j] = 0;
	}//构造函数,初始化为0
	array(int a,int b,int c,int d,int e,int f) //构造函数,形参值赋值给var数组
	{
		var[0][0]=a;
		var[0][1]=b;
		var[0][2]=c;
		var[1][0]=d;
		var[1][1]=e;
		var[1][2]=f;
	}
	void set_array( );            //由键盘输入数组的值
	void display();               //显示数组的值
	array operator+(array &X)//将两个数组相加
	{
		array x1;
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				x1.var[i][j] = var[i][j]+X.var[i][j];
		return x1;
	}
	array operator-(array &X)   //将两个数组相减
	{
		array x1;
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				x1.var[i][j] = var[i][j]-X.var[i][j];
		return x1;
	}
private:
	int var[row][col];
}; 
void array::set_array ()
{
	for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				cin>>var[i][j];
}
void array::display()
{
	for(int i = 0; i <2; i++)
		{
			for(int j = 0; j <3; j++)
			{
				cout<			}
		}
		cout<

运行结果:

1 2 3 4 5 6
Display object X
11 22 33 44 55 66
Display object Y
1 2 3 4 5 6
 Display object Z=X+Y
12 24 36 48 60 72
 Display object Z=X-Y
10 20 30 40 50 60


3.用友元函数重载运算符“+”和“-”将两个二维数组相加和相减。

#include  
using namespace std;
const int row=2; 
const int col=3;
class array 
{  
public:
	array()
	{
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				var[i][j] = 0;
	}//构造函数,初始化为0
	array(int a,int b,int c,int d,int e,int f) //构造函数,形参值赋值给var数组
	{
		var[0][0]=a;
		var[0][1]=b;
		var[0][2]=c;
		var[1][0]=d;
		var[1][1]=e;
		var[1][2]=f;
	}
	void set_array( );            //由键盘输入数组的值
	void display();               //显示数组的值
	friend array operator+(array &X, array &Y)//将两个数组相加(友元)
	{
		array x1;
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				x1.var[i][j] =X.var[i][j]+Y.var[i][j];
		return x1;
	}
	friend array operator-(array &X, array &Y)   //将两个数组相减(友元)
	{
		array x1;
		for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				x1.var[i][j] =X.var[i][j]-Y.var[i][j];
		return x1;
	}
private:
	int var[row][col];
}; 
void array::set_array ()
{
	for(int i = 0; i <2; i++)
			for(int j = 0; j <3; j++)
				cin>>var[i][j];
}
void array::display()
{
		for(int i = 0; i <2; i++)
		{
			for(int j = 0; j <3; j++)
			{
				cout<

运行结果:

1 2 3 4 5 6
Display object X
11 22 33 44 55 66
Display object Y
1 2 3 4 5 6
 Display object Z=X+Y
12 24 36 48 60 72
 Display object Z=X-Y
10 20 30 40 50 60


你可能感兴趣的:(运算符重载问题)