运算符重载例子分数加减乘除,赋值,输入输出比较大小

把老师代码上传一下,方便用移动设备时刻查看

//有理数类的运算符重载 
 
#include <iostream>
using namespace std;
class Rational{
	private:
		int fz;
		int fm;
	public:
		static int count; //静态成员,实现对象计数 
		Rational();
		Rational(const Rational& ); //拷贝构造函数,以该类对象的常引用作为形参 
		~Rational(); //析构函数 
		friend Rational operator+(Rational,Rational);  //友元函数重载形式 
		Rational operator*(Rational);                  //成员函数重载形式,隐含的this指针作为第一个形参 
		Rational& operator=(Rational);                 //赋值运算符只能重载为成员函数 
		Rational& operator++();
		Rational operator++(int);                      //后置++,添加int为参数 
		friend bool operator<(Rational,Rational);                 
		friend ostream& operator<<(ostream&,const Rational&); //输出运算符只能重载为友元函数 
		friend istream& operator>>(istream&,Rational&);//输入运算符只能重载为友元函数 
}; 
int Rational::count = 0;  //静态成员在类外进行初始化

Rational::Rational()
{
	fz =0 ;
	fm =1 ;
	count++;   //对象被创建时,计数自动+1 
}
Rational::Rational(const Rational& r)
{
	fz = r.fz;
	fm = r.fm;
	count++; //用已有对象初始化新对象,计数自动+1 
}
Rational::~Rational()
{
	count--; //对象销毁,计数自动-1 
}

ostream& operator<<(ostream& output,const Rational& x) //返回值、第一个形参都可以看做是输出流对象cout 
{
	output << x.fz << "/" << x.fm;
	return output;                              //必须返回output,支持下一次的输出 
}
istream& operator>>(istream& input,Rational& x) //第二个参数必须为引用形参,才能改变实参 
{
	input >> x.fz >> x.fm;
	return input;                               //必须返回input,支持下一次的输入
}
Rational Rational::operator*(Rational y)  //成员函数名前加类名和域作用符(::) 
{
	Rational result;
	result.fz = this->fz * y.fz;    //this指针指代乘法运算的左对象 
	result.fm = this->fm * y.fm;
	return result;
}
Rational operator+(Rational x,Rational y) //友元函数,所有操作对象都被声明为形参 
{
	Rational result;
	result.fz = x.fz * y.fm + x.fm * y.fz;
	result.fm = x.fm * y.fm;
	return result;
} 
Rational& Rational::operator=(Rational y)
{
	this->fm = y.fm;
	this->fz = y.fz;
	return *this;        //返回当前对象,返回值类型为引用 
}
Rational& Rational::operator++() //前置自增,操作对象和返回对象都是*this 
{
	this->fz += this->fm;
	return *this;            
}
Rational Rational::operator++(int) //后置自增,以int为形参,返回原对象的值 
{
	Rational temp(*this);   //保存原对象的值 
	this->fz += this->fm;     
	return temp;             //返回原对象的值 
}
bool operator<(Rational x,Rational y)
{
	if((x.fz*y.fm - x.fm*y.fz) < 0 )
		return true;
	else
		return false;     //有返回值要求时,if-else要完整匹配 
} 


int main()
{
	Rational r,b,c;  //调用无参构造函数 
    cout << "请输入四个整数,表示两个分数的分子和分母:" ;
	cin >> b >> c;   //调用输入运算符重载函数 
    cout << "b=" << b << ", c=" << c << '\n'; //调用输出运算符重载函数
    
	r = b + c;      //调用+和=运算符重载函数 
	cout << "r = b + c = " << r << '\n';

	r = b * c;     //调用*和=运算符重载函数
	cout << "r = b * c = " << r << '\n';
	
	cout << "r++ = " << (r++) << ", r = " << r << '\n';  //调用后置++运算符重载函数
	cout << "++r = " << (++r) << ", r = " << r << '\n';  //调用前置++运算符重载函数
	
	Rational num1 = r++;
	Rational num2 = r;
	Rational num3 = ++r; 
	if(num1 < num2)                      //调用<运算符重载函数
		cout << "后置自增返回原对象的值\n";
	else
		cout << "后置自增返回自增后的对象值\n";
	if(num2 < num3)
		cout << "前置自增返回自增后的对象值\n";
	else
		cout << "前置自增返回原对象的值\n";
	
	return 0;
}
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值