专题:C++常见最全类和对象中运算符的重载+完整代码

C++中运算符和输入输出流的重载实践
本文详细介绍了C++中运算符重载的两种方法,即成员函数法和类外友元函数法,并通过复数类实例展示了加减乘除以及大于、小于、等于和不等于运算符的重载。同时,文章还讲解了输入输出流的重载,包括输入流和输出流的友元函数实现方式,以读取和显示复数类对象为例。

目录

一.运算符重载

1.1.“+”“-”“*”“/”重载

成员函数实现方法:

类外友元函数实现方法:

1.2“>”“<”重载

成员函数实现方法

类外友元函数实现方法 

1.3“==”和“!=”的重载

成员函数实现方法

类外友元函数实现方法

二.输入输出流重载

2.1输入流重载:

2.2输出流重载:


一.运算符重载

运算符重载一般有两种方法,分别是成员函数法类外函数友元法

成员函数法,顾名思义,就是将重载定义为类的函数成员

类外友元函数法,就是利用类外单独的函数实现重载功能,并将函数和类建立起友元关系

两种方法的显著区别就在于:

1.解决访问私有(保护)成员权限问题;

2.传入参数个数不同;(其实第二点区别派生于第一点)

下面对于每种常见的运算符的重载,都会给出两种方法的代码。

对于运算符重载,一般格式为:

1.类内成员函数:

class

{

        ……

public:

        类名 operator 将要重载的运算符 (类名同型的数据类型 &)

        {

                类名同型的数据类型  结果变量;

                计算结果;

                return 结果变量;

        }

}

int main(){}

2.类外友元函数:

class

{

        ……

public:

        ……

        friend 类名 operator 将要重载的运算符 (类名同型数据类型 &对象1,类名同型数据类型&对象2);

}

类名 operator 将要重载的运算符 (类名同型数据类型 &对象1,类名同型数据类型&对象2){

        类名同型的数据类型 结果变量;

        进行二元运算并将结果赋值给结果变量;

        返回结果变量;

}

int main (){}

以下是具体例子:

1.1.“+”“-”“*”“/”重载

成员函数实现方法:

#include <iostream>
using namespace std;
class complex
{
protected:
	double real, imag;
public:
	complex(double a = 0, double b = 0)
	{
		this->real = a;
		this->imag = b;
	}
	void showcomplex()
	{
		cout << "(" << this->real << "," << this->imag << "i)" << endl;
	}
	//重载运算符
	complex operator+(complex&);
	complex operator-(complex&);
	complex operator*(complex&);
	complex operator /(complex&);
};
inline complex complex::operator +(complex& c)
{
	complex temp;
	temp.real = this->real + c.real;
	temp.imag = this->imag + c.imag;
	return temp;
}
inline complex complex::operator -(complex& c)
{
	complex temp;
	temp.real = this->real - c.real;
	temp.imag = this->imag - c.imag;
	return temp;
}
inline complex complex::operator *(complex& c)
{
	complex temp;
	temp.real = this->real * c.real - this->imag * c.imag;
	temp.imag = this->real * c.imag + this->imag * c.real;
	return temp;
}
inline complex complex::operator/(complex& c)
{
	if (c.real == 0 && c.imag == 0)
	{
		cout << "error" << endl;
		exit(0);
	}
	else
	{
		complex temp;
		double under = c.real * c.real + c.imag * c.imag;
		temp.real = (this->real * c.real + this->imag * c.imag) / under;
		temp.imag = (this->imag * c.real - this->real * c.imag) / under;
		return temp;
	}
}
int main()
{
	system("color 0A");
	complex a(1, 2), b(2, 3), c;
	c = a + b;
	c.showcomplex();
	c = a - b;
	c.showcomplex();
	c = a * b;
	c.showcomplex();
	c = a / b;
	c.showcomplex();
	return 0;
}

类外友元函数实现方法:

#include<iostream>
using namespace std;
class complex
{
private:
	double real, imag;
public:
	complex(double a = 0, double b = 0)
	{
		this->real = a;
		this->imag = b;
	}
	void showcomplex()
	{
		cout << "(" << this->real << "," << this->imag << "i)" << endl;
	}
	friend complex operator +(complex&, complex&);
	friend complex operator -(complex&, complex&);
	friend complex operator *(complex&, complex&);
	friend complex operator /(complex&, complex&);
};
complex operator+(complex& a, complex& b)
{
	complex temp;
	temp.real = a.real + b.real;
	temp.imag = a.imag + b.imag;
	return temp;
}
complex operator-(complex& a, complex& b)
{
	complex temp;
	temp.real = a.real - b.real;
	temp.imag = a.imag - b.imag;
	return temp;
}
complex operator*(complex& a, complex& b)
{
	complex temp;
	temp.real = a.real * b.real - a.imag * b.imag;
	temp.imag = a.real * b.imag + a.imag * b.real;
	return temp;
}
complex operator/(complex& a, complex& b)
{
	if (b.real == 0 && b.imag == 0)
	{
		cout << "error" << endl;
		exit(0);
	}
	else
	{
		complex temp;
		double under = b.real * b.real + b.imag * b.imag;
		temp.real = (a.real * b.real + a.imag * b.imag) / under;
		temp.imag = (a.imag * b.real - a.real * b.imag) / under;
		return temp;
	}
}
int main()
{
	system("color 0A");
	complex a(1, 2), b(2, 3), c;
	c = a + b;
	c.showcomplex();
	c = a - b;
	c.showcomplex();
	c = a * b;
	c.showcomplex();
	c = a / b;
	c.showcomplex();
	return 0;
}

运行结果:

 

1.2“>”“<”重载

成员函数实现方法

#include<iostream>
using namespace std;
class student
{
private:
	string name;
	double scores;
public:
	student()
	{
		this->name = "unknown name";
		this->scores = 0;
	}
	student(string name, double scores)
	{
		this->name = name;
		this->scores = scores;
	}
	void operator >(student& s)
	{
		cout << "operator>" << endl;
		if (this->scores > s.scores)
			cout << "true" << endl;
		else
			cout << "false" << endl;
	}
	void operator<(student& s)
	{
		cout << "operator<" << endl;
		if (this->scores < s.scores)
			cout << "true" << endl;
		else
			cout << "false" << endl;
	}
};
int main()
{
	system("color 0A");
	student a("zhang", 100);
	string name;
	double scores;
	cin >> name >> scores;
	student b(name, scores);
	a > b;
	a < b;
	return 0;
}

类外友元函数实现方法 

#include<iostream>
using namespace std;
class student
{
private:
	string name;
	double scores;
public:
	student()
	{
		this->name = "unknown name";
		this->scores = 0;
	}
	student(string name, double scores)
	{
		this->name = name;
		this->scores = scores;
	}
	friend void operator >(student& a, student& b);
	friend void operator <(student& a, student& b);
};
void operator >(student& a, student& b)
{
	cout << "operator>" << endl;
	if (a.scores > b.scores)
		cout << "true" << endl;
	else
		cout << "false" << endl;
}
void operator <(student& a, student& b)
{
	cout << "operator>" << endl;
	if (a.scores < b.scores)
		cout << "true" << endl;
	else
		cout << "false" << endl;
}
int main()
{
	system("color 0A");
	student a("zhang", 100);
	string name;
	double scores;
	cin >> name >> scores;
	student b(name, scores);
	a > b;
	a < b;
	return 0;
}

运行结果:

 

 

1.3“==”和“!=”的重载

成员函数实现方法

#include<iostream>
using namespace std;
class student
{
private:
	int ID;
	string name;
public:
	student()
	{
		this->ID - 0;
		this->name = "unknown name";
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
	}
	bool operator ==(student& s)
	{
		return ((this->ID == s.ID) && (this->name == s.name)) ? true : false;
	}
	bool operator !=(student& s)
	{
		return ((this->ID != s.ID) || (this->name != s.name)) ? true : false;
	}
};
int main()
{
	system("color 0A");
	student a(1, "zhang");
	int ID; string name;
	while(1)
	{
		cin >> ID >> name;
		student b(ID, name);
		cout << "判断a==b:" << (a == b) << endl;
		cout << "判断a!=b:" << (a != b) << endl;
	}
	return 0;
}

类外友元函数实现方法

#include<iostream>
using namespace std;
class student
{
private:
	int ID;
	string name;
public:
	student()
	{
		this->ID - 0;
		this->name = "unknown name";
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
	}
	friend bool operator ==(student&, student&);
	friend bool operator !=(student&, student&);
};
bool operator ==(student& s1, student& s2)
{
	return ((s1.ID == s2.ID) && (s1.name == s2.name)) ? true : false;
}
bool operator!=(student& s1, student& s2)
{
	return ((s1.ID != s2.ID) || (s1.name != s2.name)) ? true : false;
}
int main()
{
	system("color 0A");
	student a(1, "zhang");
	int ID; string name;
	while(1)
	{
		cin >> ID >> name;
		student b(ID, name);
		cout << "判断a==b:" << (a == b) << endl;
		cout << "判断a!=b:" << (a != b) << endl;
	}
	return 0;
}

二.输入输出流重载

输入输出流重载方法比较固定,一般格式为:

1.输入流重载:

class 类名

{

protected://或者private

        ……;

public:

        ……;

        friend istream & operator >>(istream&,类名同型数据类型&);

}

istream& operator >>(istream& 变量名1,类名同型数据类型& 变量名2)

{

        ……;//实现特定功能的代码

        return 变量名1;

}

2.输出流重载:

class 类名

{

protected://或者private

        ……;

public:

        ……;

        friend ostream &operator <<(ostream&,类名同型数据类型&);

}

ostream& operator <<(ostream& 变量名1,类名同型数据类型& 变量名2)

{

        ……;//实现特定功能的代码

        return 变量名2;

}

2.1输入流重载:

#include <iostream>
using namespace std;
class complex
{
protected:
	double real, imag;
public:
	complex(double a = 0, double b = 0)
	{
		this->real = a;
		this->imag = b;
	}
	void showcomplex()
	{
		cout << "(" << this->real << "," << this->imag << "i)" << endl;
	}
	//重载运算符
	complex operator+(complex&);
	complex operator-(complex&);
	complex operator*(complex&);
	complex operator/(complex&);
	//重载输入输出流
	friend istream& operator >> (istream&, complex&);

};
inline complex complex::operator +(complex &c)
{
	complex temp;
	temp.real = this->real + c.real;
	temp.imag = this->imag + c.imag;
	return temp;
}
inline complex complex::operator-(complex& c)
{
	complex temp;
	temp.real = this->real - c.real;
	temp.imag = this->imag - c.imag;
	return temp;
}

inline complex complex::operator*(complex& c)
{
	complex temp;
	temp.real = this->real * c.real - this->imag * c.imag;
	temp.imag = this->real * c.imag + this->imag * c.real;
	return temp;
}
inline complex complex::operator/(complex& c)
{
	complex temp;
	double under = c.real * c.real + c.imag * c.imag;
	if (under == 0)
	{
		cout << "error";
		exit(0);
	}
	temp.real = (this->real * c.real + this->imag * c.imag) / under;
	temp.imag = (this->imag * c.real - this->real * c.imag) / under;
	return temp;
}

istream& operator >> (istream& input, complex& c)
{
	cout << "请分别输入一个复数的实部和虚部:" << endl;
	input >> c.real >> c.imag;
	return input;
}
int main()
{
	complex a, b, c;
	cin >> a >> b;
	c = a + b; c.showcomplex();
	c = a - b; c.showcomplex();
	c = a * b; c.showcomplex();
	c = a / b; c.showcomplex();
	return 0;
}

2.2输出流重载:

#include <iostream>
using namespace std;
class complex
{
protected:
	double real, imag;
public:
	complex(double a = 0, double b = 0)
	{
		this->real = a;
		this->imag = b;
	}
	void showcomplex()
	{
		cout << "(" << this->real << "," << this->imag << "i)" << endl;
	}
	//重载运算符
	complex operator+(complex&);
	complex operator-(complex&);
	complex operator*(complex&);
	complex operator/(complex&);
	//重载输入输出流
	friend istream& operator >> (istream&, complex&);
	friend ostream& operator<<(ostream&, complex&);

};
inline complex complex::operator +(complex &c)
{
	complex temp;
	temp.real = this->real + c.real;
	temp.imag = this->imag + c.imag;
	return temp;
}
inline complex complex::operator-(complex& c)
{
	complex temp;
	temp.real = this->real - c.real;
	temp.imag = this->imag - c.imag;
	return temp;
}

inline complex complex::operator*(complex& c)
{
	complex temp;
	temp.real = this->real * c.real - this->imag * c.imag;
	temp.imag = this->real * c.imag + this->imag * c.real;
	return temp;
}
inline complex complex::operator/(complex& c)
{
	complex temp;
	double under = c.real * c.real + c.imag * c.imag;
	if (under == 0)
	{
		cout << "error";
		exit(0);
	}
	temp.real = (this->real * c.real + this->imag * c.imag) / under;
	temp.imag = (this->imag * c.real - this->real * c.imag) / under;
	return temp;
}

istream& operator >> (istream& input, complex& c)
{
	cout << "请分别输入一个复数的实部和虚部:" << endl;
	input >> c.real >> c.imag;
	return input;
}
ostream& operator<<(ostream& output, complex& c)
{
	cout << "(" << c.real << "," << c.imag << "i)" << endl;
	return output;
}
int main()
{
	complex a, b, c;
	cin >> a >> b;
	c = a + b; cout << c;
	c = a - b; cout << c;
	c = a * b; cout << c;
	c = a / b; cout << c;
	return 0;
}
visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青山入墨雨如画

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值