C++快速入门---运算符重载(18)

本文详细介绍了C++中的运算符重载概念,通过实例展示了如何重载基本算术运算符来实现复数和有理数的运算,涵盖了成员函数、友元函数及简化分数的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++快速入门---运算符重载(18)

 

重载就是重新赋予新的含义。函数重载是对一个已有的函数赋予新的含义,使之实现新功能。

运算符重载是通过定义函数实现的。运算符重载实质上是函数的重载。

 

重载运算符的函数一般格式如下:

函数类型 operator 运算符名称(形参列表)

{

对运算符的重载处理

}

例如我们可以重载运算符+,如下:

int operator+(int a, int b)

{

return (a-b);

}

 

实际上,运算符重载函数有两个参数,但由于重载函数是Complex类中的成员函数,有一个参数是隐含着的,运算符函数是用this指针隐式地访问类对象的成员。

return Complex(real+c2.real, imag+c2.imag);

return Complex(this->real+c2.real, this->imag+c2.imag);

return Complex(c1.real+c2.real, c1.imag+c2.imag);

 

那么例子中的c1+c2,编译系统把它解释为:c1.operator+(c2)

即通过对象c1调用运算符重载函数,并以表达式中第二个参数(运算符右侧的类对象c2)作为函数实参。

 

实现复数加法:(不使用重载)

#include <iostream>

class Complex
{
public:
	Complex();
	Complex(double r, double i);
	Complex complex_add(Complex &d);//实现加法 
	void print();

private:
	double real;
	double imag;
};

Complex::Complex()
{
	real = 0;//实部 
	imag = 0;//虚部 
}

Complex::Complex(double r, double i)
{
	real = r;
	imag = i;
}

//对+进行重载,参数是被加数 
Complex Complex::complex_add(Complex &d)
{
	Complex c;
	
	c.real = real + d.real;
	c.imag = imag + d.imag;
	
	return c;
}

void Complex::print()
{
	std::cout << "(" << real << ", " << imag << "i)\n";
}

int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	
	c3 = c1.complex_add(c2);
	
	std::cout << "c1 = ";
	c1.print();
	std::cout << "c2 = ";
	c2.print();
	std::cout << "c1 + c2= ";
	c3.print();
	
	return 0;
}

 

 

实现复数加法:(使用重载)

#include <iostream>

// 演示对运算符“+”进行重载达到目的!

class Complex
{
public:
	Complex();
	Complex(double r, double i);
	Complex operator+(Complex &d);
	void print();

private:
	double real;
	double imag;
};

Complex::Complex()
{
	real = 0;
	imag = 0;
}

Complex::Complex(double r, double i)
{
	real = r;
	imag = i;
}

//对+进行重载,参数是被加数 
Complex Complex::operator+(Complex &d)
{
	Complex c;
	
	c.real = real + d.real;
	c.imag = imag + d.imag;
	
	return c
}

void Complex::print()
{
	std::cout << "(" << real << ", " << imag << "i)\n";
}

int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	
	c3 = c1 + c2;
	
	std::cout << "c1 = ";
	c1.print();
	std::cout << "c2 = ";
	c2.print();
	std::cout << "c1 + c2= ";
	c3.print();
	
	return 0;
}

 

运算符函数作为友元函数:

#include <iostream>

// 演示对运算符“+”进行重载达到目的!

class Complex
{
public:
	Complex();
	Complex(double r, double i);
	//friend 这个函数不属于这个类,属于这个类的朋友,亲戚 
	friend Complex operator+(Complex &c, Complex &d);
	void print();

private:
	double real;
	double imag;
};

Complex::Complex()
{
	real = 0;
	imag = 0;
}

Complex::Complex(double r, double i)
{
	real = r;
	imag = i;
}

//对+进行重载,参数是被加数
//注意,这里作为友元函数,不属于Complex,记得别写 
Complex operator+(Complex &c, Complex &d)
{
	return Complex(c.real+d.real, c.imag+d.imag);
}

void Complex::print()
{
	std::cout << "(" << real << ", " << imag << "i)\n";
}

int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	
	c3 = c1 + c2;
	
	std::cout << "c1 = ";
	c1.print();
	std::cout << "c2 = ";
	c2.print();
	std::cout << "c1 + c2= ";
	c3.print();
	
	return 0;
}

 

重载运算符“+”,“-”,“*”,“/”,实现有理数的加减乘除运算。

#include <iostream>
#include <string>
#include <stdlib.h>
class Rational
{
public:
	Rational(int num, int denom);	//num = 分子,denom = 分母
	
	Rational operator+(Rational rhs);	//rhs == right hand side
	Rational operator-(Rational rhs);
	Rational operator*(Rational rhs);
	Rational operator/(Rational rhs);
	
	void print();
	
private:
	void normalize();	//负责对分数的简化处理
	
	int numerator;		//分子
	int denominator;	//分母 
};

Rational::Rational(int num, int denom)
{
	numerator = num;
	denominator = denom;
	
	normalize();
}

//normalize()对分数进行简化操作包括:
//1. 只允许分子为负数,如果分母为负数则把负数挪到分子部分,如 1/-2 = -1/2
//2. 利用欧几里德算法(辗转求余原理:将分数进行简化,2/10 => 1/5)
void Rational::normalize()
{
	//确保分母为正
	if(denominator < 0)
	{
		numerator = -numerator;
		denominator = -denominator;
	}
	
	//欧几里德算法
	int a = abs(numerator);
	int b = abs(denominator);
	
	//求出最大公约数
	while (b > 0)
	{
		int t = a % b;
		a = b;
		b = t;
	}
	 
	//分子、分母分别除以最大公约数得到最简化分数
	numerator /= a;
	denominator /= a;
}

// a   c   a*d   c*b   a*d + c*b
// - + - = --- + --- = ---------
// b   d   b*d   b*d      b*d
Rational Rational::operator+(Rational rhs)
{
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;
	
	int e = a*b + c*b;
	int f = b*d;
	
	return Rational(e, f);
}

// a   c   a   -c
// - - - = - + ---
// b   d   b    d
Rational Rational::operator-(Rational rhs)
{
	rhs.numerator = -rhs.numerator;
	
	return operator+(rhs);
}

// a   c   a*c
// - * - = ---
// b   d   b*d
Rational Rational::operator*(Rational rhs)
{
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;
	
	int e = a*c;
	int f = b*d;
	
	return Rational(e, f);
}

// a   c   a    d
// - / - = - * ---
// b   d   b    c
Rational Rational::operator/(Rational rhs)
{
	int t = rhs.numerator;
	rhs.numerator = rhs.denominator;
	rhs.denominator = t;
	
	return operator*(rhs);
}

void Rational::print()
{
	if(numerator % denominator == 0)
		std::cout << numerator / denominator;
	else
		std::cout << numerator << "/" << denominator; 
}

int main()
{
	Rational f1(2, 16);
	Rational f2(7, 8);
	
	//测试有理数加法运算
	Rational res = f1 + f2;
	f1.print();
	std::cout << "+";
	f2.print();
	std::cout << "=";
	res.print();
	std::cout << "\n"; 
	
	//测试有理数减法运算
	res = f1 - f2;
	f1.print();
	std::cout << "-";
	f2.print();
	std::cout << "=";
	res.print();
	std::cout << "\n"; 
	
	//测试有理数乘法运算
	res = f1 * f2;
	f1.print();
	std::cout << "*";
	f2.print();
	std::cout << "=";
	res.print();
	std::cout << "\n";
	
	//测试有理数除法运算
	res = f1 / f2;
	f1.print();
	std::cout << "/";
	f2.print();
	std::cout << "=";
	res.print();
	std::cout << "\n";  
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值