Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

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

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

//有理数类的运算符重载 
 
#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;
}

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 在Ardunio IDE完成STM32 F103指南者的串口通信

下一篇: 来自一位双非本科跌跌撞撞的秋招指南(秋招攻略/经验分享/干货)

精华推荐