C++学习笔记——03 对运算符进行重载

文章目录

      • 一、为什么要对运算符重载
      • 二、对运算符重载的方法
      • 三、重载运算符的规则
      • 四、运算符重载函数作为类成员函数和友元函数
      • 五、重载双目运算符
      • 六、重载单目运算符
      • 七、重载流插入运算符和流提取运算符
          • 1.重载流插入符“<<”
          • 2.重载流提取符“>>”
      • 八、不同类型数据间的转换
          • 1.标准类型数据间的转换
          • 2.把其他类型转换为类对象——用转换构造函数
          • 3.将类对象转换为其他类型数据——用类型转换函数

前言:本系列笔记是主要是对于谭浩强的c++程序设计的学习笔记再加上一些我自己的理解。如果有误欢迎大家指出。

建议是有基础的同学快速入门,或者复习用

一、为什么要对运算符重载

#include
using namespace std;
class Complex
{
	public:
		Complex(){real=0;imag=0;}
		Complex(double r,double i){real=r;imag=i;}
		Complex complex_add(Complex &c2);
		void display();
	private:
		double real;
		double imag;
};
Complex Complex::complex_add(Complex &c2)
{
	Complex c;
	c.real=real+c2.real;
	c.imag=imag+c2.imag;
	return c;
}
void Complex::display()
{
	cout<<"("<#include
using namespace std;
class Complex
{
	public:
		Complex(){real=0;imag=0;}
		Complex(double r,double i){real=r;imag=i;}
		Complex operator+(Complex &c2);
		void display();
	private:
		double real;
		double imag;
};
Complex Complex::operator+(Complex &c2)
{
	Complex c;
	c.real=real+c2.real;
	c.imag=imag+c2.imag;
	return c;
}
void Complex::display()
{
	cout<<"("<#include
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 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);
}
/*Complex Complex::operator+(Complex &c2)
{
	Complex c;
	c.real=real+c2.real;
	c.imag=imag+c2.imag;
	return c;
}*/
void Complex::display()
{
	cout<<"("<#include
using namespace std;
class String
{
	public:
		String(){p=NULL;}
		String(char *str);
		void display();
	private:
		char *p;
};
String::String(char *str)
{
	p=str;
}
void String::display()
{
	cout<
#include
#include
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()
{
	cout<(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)<
#include
#include
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()
{
	cout<(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();
		cout<

六、重载单目运算符

#include
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<=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++;
	cout<<"time1++ : ";
	time1.display();
	cout<<"time2 : ";
	time2.display();
	
}

七、重载流插入运算符和流提取运算符

用户自己定义的类型的数据(如类对象),是不能直接用“<<‘和”>>“输入和输出的,如果想用它们输出和输入自己声明的类型的数据,必须对它们重载。

重载的函数形式如下:

istream & operator>>(istream&,自定义类&);

ostream & operator<<(ostream&,自定义类&);

只能将重载“<<‘和”>>“的函数作为友元函数而不能定义为成员函数

1.重载流插入符“<<”
#include
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++规定运算符“<<”重载函数的第一个参数和函数的类型都必须是ostream类型的引用,是为了返回cout的当前值以便连续输出。

2.重载流提取符“>>”
#include
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<<"("<=0)output<<"+";
	output<>(istream & input,Complex& c)
{
	cout<<"input real part and imaginary part of complex number:";
	input>>c.real>>c.imag;
	return input;
}
int main()
{
	Complex c1,c2;
	cin>>c1>>c2;
	cout<<"c1="<Complex(double r){real=r;imag=0;}

然后就可以在类的作用域中用以下方式进行转换:

类名(指定类型的数据)

Complex(double r)
3.将类对象转换为其他类型数据——用类型转换函数

类型转换函数的一般形式:

operator 类型名 ()

{实现转换的语句}

在函数名前不能指定函数类型,函数没有参数。且其只能作为成员函数,不能是友元函数或普通函数,因为转换的主体是本类的对象。

#include
using namespace std;
class Complex
{
	public:
		Complex(){real=0;imag=0;}
		Complex(double r,double i){real=r;imag=i;}
		operator double(){return real;}
	private:
		double real;
		double imag;
};
int main()
{
	Complex c1(3,4),c2(5,-10),c3;
	double d;
	d=2.5+c1;
	cout<
#include
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<<"("<

在已经定义了相应的转换构造函数情况下,将运算符“+”函数重载为友元函数,在进行两个复数相加时可以用交换律。

你可能感兴趣的:(c++,学习,笔记)