运算符的重载



c++中运算符的重载:

#include <iostream>   
using namespace std;   
class ADD{ //定义类add
public:
	int operand;
	ADD(){  //定义构造函数
	operand=0;
	}
	ADD(int value){  //重载构造函数
	operand=value;
	}
};
ADD operator+(ADD a,int b){  //重载+运算法,操作add类
ADD sum;   //创建对象
sum.operand=a.operand + b;  //实现类中数据成员与整形变量的相加
return sum;
}

int main(){
	ADD a(5),b;   //创建对象
	b=a + 8;     //调用重载后的“+”运算符
	cout<<"this sum is :"<<b.operand<<endl;
return 0;
}

复数运算运算符重载:

#include <iostream>

class Complex //复数类
{
public:
	double real;//实数
	double imag;//虚数
	Complex(double real=0,double imag=0)
	{
		this->real=real;
		this->imag=imag;
	}
};

Complex operator+(Complex com1,Complex com2)//运算符重载函数
{
	return Complex(com1.real+com2.real,com1.imag+com2.imag);
}

int main()
{
	Complex com1(10,10),com2(20,20),sum;
	sum=com1+com2;//或sum=operator+(com1,com2)
	
	std::cout<<"sum的实数部分为"<<sum.real<<std::endl;
	std::cout<<"sum的虚数部分为"<<sum.imag<<"i"<<std::endl;
	
	return 0;
}

用友元函数运算符重载求复数:

#include <iostream>
using namespace std;

class Complex{

public:
Complex( ){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator + (Complex &c1,Complex &c2);//重载函数作为友元函数
void display( );
private:
	double real;
	double imag;
};

Complex operator + (Complex &c1,Complex &c2){ //定义作为友元函数的重载函数
	return Complex(c1.real+c2.real, c1.imag+c2.imag);
}

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

int main( ){

	Complex c1(3,4),c2(5,-10),c3;
	c3=c1+c2;
	cout<<"c1="; c1.display( );
	cout<<"c2="; c2.display( );
	cout<<"c1+c2 ="; c3.display( );
	system("pause");
}

重载一个运算符">" 比较字符串

#include <iostream>
#include <string>
using namespace std;
class String
{public:
String( ){
	p=NULL;
}
String(char *str);
friend bool operator>(String &string1,String &string2);//声明运算符函数为友元函数
void display( );
private:
	char *p;//字符型指针,用于指向字符串
};
String::String(char *str){
	p=str;
}


void String::display( ){  //输出p所指向的字符串
cout<<p;
}
bool operator>(String &string1,String &string2){ //定义运算符重载函数
if(strcmp(string1.p,string2.p)>0)
return true;
else return false;
}

int main( )
{String string1("Hello"),string2("Book");
cout<<(string1>string2)<<endl;
}

strcmp函数 
其一般形式为:strcmp(字符串1,字符串2) 
strcmp的作用是比较字符串1和字符串2。
例如:strcmp(str1,str2);
      strcmp(″China″,″Korea″);
      strcmp(str1,″Beijing″);
比较的结果由函数值带回
(1) 如果字符串1=字符串2,函数值为0。
(2) 如果字符串1>字符串2,函数值为一正整数。
(3) 如果字符串1<字符串2,函数值为一负整数。
注意:对两个字符串比较,不能用以下形式:
if(str1>str2) printf(″yes″);
而只能用
if(strcmp(str1,str2)>0) printf(″yes″);


重载运算符">","<","=" 比较字符串

#include <iostream>
using namespace std;
class String{
public:
	String( ){
		p=NULL;
	}
	String(char *str);
	friend bool operator>(String &string1,String &string2);
	friend bool operator<(String &string1,String &string2);
	friend bool operator==(String &string1,String &string2);

	void display( );
private:
	char *p;
}; 
String::String(char *str){
	p=str;
}

void String::display( ){  //输出p所指向的字符串
	cout<<p;
}

bool operator>(String &string1,String &string2){
	if(strcmp(string1.p,string2.p)>0)
		return true;
	else
		return false;
}

bool operator<(String &string1,String &string2){
	if(strcmp(string1.p,string2.p)<0)
		return true;
	else
		return false;
}

bool operator==(String &string1,String &string2){
	if(strcmp(string1.p,string2.p)==0)
		return true;
	else
		return false;
}

void compare(String &string1,String &string2){
	if(operator>(string1,string2)==1){
		string1.display( );cout<<">";string2.display( );
	}
	else
		if(operator<(string1,string2)==1){
			string1.display( );cout<<"<";string2.display( );
		}
		else
			if(operator==(string1,string2)==1){
				string1.display( );cout<<"=";string2.display( );
			}
			cout<<endl;
}
int main( ){
	String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
	compare(string1,string2);
	compare(string2,string3);
	compare(string1,string4);
	system("pause");
return 0;
}


有一个Time,包含数据成员minute()sec(),模拟秒表,每次走一秒,60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值:


#include <iostream>
using namespace std;
class Time{
public:
	Time( ){minute=0;sec=0;}  //默认构造函数
	Time(int m,int s):minute(m),sec(s){ } //构造函数重载
	Time operator++( ); //声明运算符重载函数
	void display( ){
		cout<<minute<<":"<<sec<<endl;  //定义输出时间函数
	} 
private:
	int minute;
	int sec;
};
Time Time::operator++( ) { //定义运算符重载函数
	if(++sec>=60){
		sec-=60; //满60秒进1分钟
		++minute;
	}
return *this;  //返回当前对象值
}
int main( ){
	Time time1(34,0);
	for (int i=0;i<61;i++){
		++time1;
		time1.display( );
	}
	system("pause");
return 0;
}


重载++ 和++重载 time类

#include <iostream>
using namespace std;
class Time{
public:
	Time( ){minute=0;sec=0;}
	Time(int m,int s):minute(m),sec(s){}

	Time operator++( );//声明前置自增运算符“++”重载函数
	Time operator++(int);//声明后置自增运算符“++”重载函数
	void display( ){
		cout<<minute<<":"<<sec<<endl;
	}
private:
	int minute;
	int sec;
}; 

Time Time::operator++( )  { //定义前置自增运算符“++”重载函数
	if(++sec>=60){
		sec-=60;
		++minute;
	}
return *this;//返回自加后的当前对象
}

Time Time::operator++(int){  //定义后置自增运算符“++”重载函数

	Time temp(*this);
	sec++;
	if(sec>=60){
		sec-=60;
		++minute;
	}
return temp;  //返回的是自加前的对象
}

int main( ){
	Time time1(34,59),time2;
	cout<<" time1 : ";
	time1.display( );
	++time1;
	cout<<" ++time1: ";
	time1.display( );
	time2=time1++;  //将自加前的对象的值赋给time2
	cout<<" time1++: ";
	time1.display( );
	cout<<" time2 : ";
	time2.display( ); //输出time2对象的值
	system("pause");
	return 0;
}

重载<<方法得到复数

//重载流插入运算符“<<”
#include <iostream>
using namespace std; 
class Complex
{public:
Complex( ){
	real=0;imag=0;
}
Complex(double r,double i){
	real=r;imag=i;
}
Complex operator + (Complex &c2);  //运算符“+”重载为成员函数
friend ostream& operator << (ostream&,Complex&);  //运算符“<<”重载为友元函数
private:
	double real;
	double imag;
};

Complex Complex::operator + (Complex &c2) {//定义运算符“+”重载函数

	return Complex(real+c2.real,imag+c2.imag);
}
ostream& operator << (ostream& output,Complex& c){ //定义运算符“<<”重载函数
	output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
	return output;
}

int main( ){
	Complex c1(2,4),c2(6,10),c3;
	c3=c1+c2;
	cout<<c3;
	return 0;
}


重载<< 和>>方法计算复数

#include <iostream>
using namespace std;
class Complex{
public:
	friend ostream& operator << (ostream&,Complex&); //声明重载运算符“<<”
	friend istream& operator >> (istream&,Complex&); //声明重载运算符“>>”
private:
	double real;
	double imag;
};
ostream& operator << (ostream& output,Complex& c){ //定义重载运算符“<<”
	output<<"("<<c.real<<"+"<<c.imag<<"i)";
return output;
}
istream& operator >> (istream& input,Complex& c){  //定义重载运算符“>>”
	cout<<"input number:";
	input>>c.real>>c.imag;
return input;
}
int main( ){
	Complex c1,c2;
	cin>>c1>>c2; 
	cout<<"c1="<<c1<<endl; 
	cout<<"c2="<<c2<<endl;
	system("pause");
return 0;
}

重载函数 使用类型转换函数的简单例子

#include <iostream>
using namespace std;
class Complex
{public:
Complex( ){real=0;imag=0;zz=1;}
Complex(double r,double i){real=r;imag=i;}
operator double( ) {return real;} //类型转换函数
private:
double real;
double imag;
int zz;
};

int main( )
{Complex c1(3,4),c2(5,-10),c3;
double d;
d=2.5+c1;//要求将一个double数据与Complex类数据相加
cout<<d<<endl;
return 0;
}

包含转换构造函数、运算符重载函数和类型转换函数


#include <iostream>
using namespace std;
class Complex
{public:
Complex( ){real=0;imag=0;}  //默认构造函数
Complex(double r){real=r;imag=0;}//转换构造函数
Complex(double r,double i){real=r;imag=i;}//实现初始化的构造函数
friend Complex operator + (Complex c1,Complex c2); //重载运算符“+”的友元函数
void display( );
private:
double real;
double imag;
};

Complex operator + (Complex c1,Complex c2)//定义运算符“+”重载函数
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}

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

int main( )
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+2.5; //复数与double数据相加
c3.display( );
return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值