C++重载运算符

/*
题目:
		请编写程序,处理一个复数与一个double 数相加的运算,结果存放在一个double
		型变量dl中,输出dl值,再以复数形式输出此值。定义Complex(复数)类,在成员
		函数中包含重载类型转换运算符;
		operator double(){double real};
编码日期:2012年10月27日 12:19:29
编码人:白增飞;

*/

#include <iostream.h>
using namespace std;
class Complex
{
public:
	Complex(){real = 0; imag = 0;}
	Complex(double r, double i){real =r; imag = i;}
	Complex operator+(double);
	operator double();
	void display();
private:
	double real;
	double imag;
};

void Complex::display()
{
	cout<<real<<","<<imag<<endl;
return;
}

Complex Complex::operator +(double i)
{
	return Complex(real + i, imag);
}

Complex::operator double()
{
	return real;
}

int main()
{
	Complex dl(100, 34), d2;
	double i = 10;

	double ds;
	ds= dl +i;
	cout<<"double:"<<ds<<endl;
	d2 = dl + i;
	d2.display();
	getchar();
return 0;

}

/*
	题目:
		定义一个复数类Complex,重载运算符”+“,使之用于复数加法运算。将运算符
		重载为非成员函数,非友元的普通函数。编写程序,求两个复数之和;
	
	
编码日期:2012年10月27日 11:57:49;
编码人:白增飞
*/

#include <iostream.h>
using namespace std;
class Complex
{
	public:
		Complex(){real = 0; imag = 0;}
		Complex(double r, double i){real = r; imag = i;}
		double get_real();
		double get_imag();
		void display();
	private:
		double real;
		double imag;
};

void Complex::display()
{
	cout<<real<<","<<imag<<endl;
return;
}

double Complex::get_imag()
{
	return imag;
}

double Complex::get_real()
{
	return real;
}

Complex operator+(Complex& c, Complex& c1)
{
	return Complex(c.get_real()+ c1.get_real(), c.get_imag() + c1.get_imag());
}

int main()
{
	Complex c1(100, 213), c2(34, 300), c3;
	c3 = c1 + c2;
	cout<<"c1=";c1.display();
	cout<<"c2=";c2.display();
	cout<<"c1 + c2 = ";c3.display();
getchar();
return 0;
}

/*
题目:
		1.有两个矩阵a和b,均为2行3列。求两个矩阵之和。
		重载运算符“+”,使之能用于矩阵相加(如c = a + b);

		2.重载“>>"和”<<“使之能用于矩阵的输入;

编码日期:2012年10月27日 11:32:34
编码人:白增飞;
*/
#include <iostream.h>
using namespace std;

class Matrix
{
	public:
		Matrix();
		friend Matrix operator+(Matrix&, Matrix&);
		friend istream& operator>>(istream&, Matrix&);
		friend ostream& operator<<(ostream&, Matrix&);
	   //	void input();
	   //	void display();
	private:
		int mat[2][3];
};
/*
void Matrix::display()
{
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 3; j++)
		{
			cout<<mat[i][j]<<" ";
			cout<<endl;
		}
return;
}

void Matrix::input()
{
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 3; j++)
		{
			cin>>mat[i][j];
	   }
return;
}
*/
istream&
	operator>>(istream& is,Matrix& m1)
{
	for(int i = 0; i < 2; i++)
		for(int j = 0;  j < 3; j++)
			{
				is>>m1.mat[i][j];
			}
return is;
}

ostream&
	operator<<(ostream& os, Matrix& m1)
{
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 3; j++)
		{
			os<<m1.mat[i][j]<<" ";
		}
return os;
}
Matrix operator+(Matrix& m, Matrix& m1)
{
	Matrix temp;
		for(int i=0; i < 2; i++)
			for(int j = 0; j < 3; j++)
				{
					temp.mat[i][j] = m.mat[i][j] + m1.mat[i][j];
				}
return temp;
}

Matrix::Matrix()
{
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 3; j++)
			{
				mat[i][j] = 0;
			}
return;
}

int main()
{
	Matrix m1,m2, m3;
 //	m1.input();
 //	m2.input();
	cin>>m1>>m2;

	cout<<endl<<"Matrix a:"<<endl;
	//m1.display();
	cout<<m1;

	cout<<endl<<"Matrix b:"<<endl;
	//m2.display();
	cout<<m2;

	m3 = m1 + m2;
	cout<<endl<<"m1 + m2 = "<<endl;
	//m3.display() ;
	cout<<m3;

	getchar();
	getchar();

	return 0;
}

/*
题目:
		定义一个复数类Complex,重载运算符“+”,使之能用于复数加法运算。参加
		运算的两个运算量可以都是类对象,也可以是其有一个整数,顺序任意。例如:
		c1+c2,i + c1, c1+i均合法(设i为下整数,c1, c2为复数).编写程序,分别
		求两个复数之和,整数之和复数之和;
编码日期:2012年10月27日 11:36:44
编码人:白增飞;

*/
#include <iostream.h>
using namespace std;

class Complex
{
	public:
		Complex(){real = 0; imag = 0;}
		Complex(double r, double i){real = r; imag = i;}
		friend Complex operator+(int, Complex&);
		friend Complex operator+(Complex&, Complex&);
		friend Complex operator+(Complex&, int);
		void display();
	private:
		double real;
		double imag;
};

void Complex::display()
{
	cout<<real <<","<<imag<<endl;
}

Complex operator+(Complex& c1, int c)
{
	return Complex(c1.real + c, c1.imag = 0);
}
Complex operator+(int c, Complex& c1)
{
	return Complex(c+c1.real, c1.imag = 0);
}

Complex operator+(Complex& c, Complex& c1)
{
	return Complex(c.real + c1.real, c.imag + c1.imag);
}

int main()
{
	Complex  c1(100, 34), c2, c3(23, 345);
	int i = 10;
	c2= c1+i;
	cout<<"i+c1=";c2.display();
	c2 = c1 + c3;
	cout<<"c1 + c3=";c2.display();
	getchar();

	return 0;
}

/*
题目:
		定义一个复数类Complex,重载“+”,“-”,“*”,“/"使之能用于复数的加减乘除。运算符重载函数做为Complex
		类的成员函数,编写程序,分别求出两个复数之和,之差,积和商;
编码日期:2012年10月27日 11:51:43
编码人:白增飞;

*/
#include <iostream.h>
using namespace std;

class Complex
{
	public:
		Complex(){real =0; imag = 0;}
		Complex(double r, double i){real = r; imag = i;}
		Complex operator+(Complex&);
		Complex operator-(Complex&);
		Complex operator*(Complex&);
		Complex operator/(Complex&);
		void display();
	private:
		double real;
		double imag;
};

void Complex::display()
{
	cout<<real<<","<<imag<<endl;
}
 Complex Complex::operator +(Complex& c1)
 {
	 return Complex(real+c1.real, imag+c1.imag);
 }
  Complex Complex::operator -(Complex& c1)
 {
	 return Complex(real-c1.real, imag-c1.imag);
 }
  Complex Complex::operator *(Complex& c1)
 {
	 return Complex(real*c1.real, imag*c1.imag);
 }
   Complex Complex::operator /(Complex& c1)
 {
	 return Complex(real/c1.real, imag/c1.imag);
 }

int main()
{
	Complex c1(100, 34), c2(34, 54), c3;
	c3 = c1 + c2;
	cout<<"c1+c2=";
	c3.display();

	c3 = c1 - c2;
	cout<<"c1-c2=";
	c3.display();

	c3 = c1*c2;
	cout<<"c1*c2=";
	c3.display();

	c3 = c1 / c2;
	cout<<"c1/c2=";
	c3.display();
	getchar();
	return 0;
}

C++运算符重载

一、运算符重载:
			1.什么是重载?
				 就是赋予新的含义,函数的重载就是对一个已经有的函数重载赋予新的含义,使这个函数实现新的
			功能,因此同一个函数可以实现不同的功能。
				如下代码:
						#include <iostream>
						using namespace std;
						
						int add(int, int);
						double add( double, double);
						
						int main()
						{
							int a = 10, b =20, sum;
							sum = add(a, b);
							cout<<sum;
							
							double d1 = 10.0, d2 = 11.0, dsum;
							dsum = add(d1, d2);
							cout<<dsum<<endl;
							
						return 0;
						}
					在上面的程序当中,有两个add函数,相同点都是用来求和的,不同点是可以进行两种类型的运算
				这个就是一个简单的函数重载;现在在来理解上面的定义就可以很好的理解了!
				
			2.什么是运算重载?
				  和上面函数的重载定义是一样的,就是在已有的运算符进行赋予新的含义,来实现更多的功能,这个
			功能主要还是以类为主的;
				如下面的例子:
						#include <iostream.h>
						class Complex
						{
						public:
							Complex(){real = 0; imag = 0;}
							Complex(double r, double i){real = r; imag = i;}
							Complex Complex operator+(Complex& c);  //对已有的“+”进行重载可以实现类实例的运算;并返回实例运算结果;
							void display();
						private:
							double real;
							double imag;
						};
						void Complex::display()
						{
							cout<<real<<","<<imag<<endl;
						}
						
						Complex Complex::operator+(Complex& c)
						{
							return Complex(real +c.real, imag + c.imag);
							//对"+"符号进行重载;
						}
						
						int main()
						{
							Complex c1(100, 20), c2(34, 334), c3;
							c3 = c1 + c2;
							    //通过重载我们可以象内置数据类型运算一样计算;
								//如  int a = 10, b = 20, c;  c = a +b;
							cout<<"c1=";c1.display();
							cout<<"c2=";c2.dispaly();
							cout<<"c1+c2=";c3.display();
						return 0;
						}
					上面的例子只是重载了一下“+”还有其他的运算符也是可以重载的,双目运算符,单目运算符、还有就是
				插入流运算和提取流运算符即">>"与“<<",在这里”>>“与”<<“比较特殊,在声明他们时,要把他们声明成友元
				函数,不能做为类成员,原因是有以下几点:
														1.大部份的标准库实现中,对ostream,istream类体系采用了构
															造函数保护继承的方式,致使即使以继承的方式来扩展流类,
															也会在对象实例化时遭遇阻碍。
														2.另一方面,标准库中的流类,其插入符函数没有声明为虚函数,
															因此子类不能对其实现进行覆盖,所以也使成员函数重载遭
															遇到实质的困难.
					插入与提取流操作重载函数形式:
							istream& operator>>(istream&, ClassType&);
							ostream& operator<<(ostream&, ClassType&);
						
				
					


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值