C++各个运算符重载实例
// OperatorRewrite1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; enum style_enum{ zero, one, two, three }; class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu = 0, int de = 1) :nume(nu), deno(de){} //构造函数,初始化用 void simplify(); //化简(使分子分母没有公因子) //输入输出重载 friend ostream& operator<<(ostream &out, const CFraction &cf);//必须是友元函数,相当于独立函数 friend istream& operator>>(istream &in, CFraction &cf); //加减乘除,结果需要化简 friend CFraction operator+(const CFraction &lcf, const CFraction &rcf); friend CFraction operator-(const CFraction &lcf, const CFraction &rcf); friend CFraction operator*(const CFraction &lcf, const CFraction &rcf); friend CFraction operator/(const CFraction &lcf, const CFraction &rcf); //关系运算符 friend bool operator>(const CFraction &lcf, const CFraction &rcf);//left right friend bool operator<(const CFraction &lcf, const CFraction &rcf); friend bool operator>=(const CFraction &lcf, const CFraction &rcf); friend bool operator<=(const CFraction &lcf, const CFraction &rcf); friend bool operator==(const CFraction &lcf, const CFraction &rcf); friend bool operator!=(const CFraction &lcf, const CFraction &rcf); //取+、-一目运算符 CFraction operator+(); CFraction operator-(); }; void CFraction::simplify() { int v1 = nume; int v2 = deno; while (v2)//采用 { int temp = v2; v2 = v1 % v2; v1 = temp; } nume /= v1; deno /= v1; if (deno < 0) { deno = -deno; nume = -nume; } } //输出重载 ostream& operator<<(ostream &out, const CFraction &cf) { out << cf.nume << '/' << cf.deno; return out; } //输入重载 istream& operator>>(istream &in, CFraction &cf) { char ch; while (1) { in >> cf.nume >> ch >> cf.deno; if (cf.deno == 0) cerr << "分母为0请重新输入\n"; else if (ch != '/') cerr << "格式错误(形如m/n)请重新输入\n"; else break; } return in; } //加法重载 CFraction operator+(const CFraction &lcf, const CFraction &rcf) { CFraction cf; cf.nume = lcf.nume*rcf.deno + lcf.deno*rcf.nume; cf.deno = lcf.deno*rcf.deno; cf.simplify(); return cf; } //减法重载 CFraction operator-(const CFraction &lcf, const CFraction &rcf) { CFraction cf; cf.nume = lcf.nume*rcf.deno - rcf.nume*lcf.deno; cf.deno = lcf.deno*rcf.deno; cf.simplify(); return cf; } //乘法重载 CFraction operator*(const CFraction &lcf, const CFraction &rcf) { CFraction cf; cf.nume = lcf.nume*rcf.nume; cf.deno = lcf.deno*rcf.deno; cf.simplify(); return cf; } //除法重载 CFraction operator/(const CFraction &lcf, const CFraction &rcf) { CFraction cf; cf.nume = lcf.nume*rcf.deno; cf.deno = lcf.deno*rcf.nume; cf.simplify(); return cf; } //取正重载 CFraction CFraction::operator+() { simplify(); if (nume < 0) nume = -nume; return *this; } //取负重载 CFraction CFraction::operator-() { simplify(); nume = -nume; return *this; } //大于号重载 bool operator>(const CFraction &lcf, const CFraction &rcf) { int l_nume = lcf.nume * rcf.deno;//通分后相减 int r_nume = rcf.nume * lcf.deno; int common_deno = lcf.deno * rcf.deno; if ((l_nume - r_nume) * common_deno > 0) return true; return false; } //小于号重载 bool operator<(const CFraction &lcf, const CFraction &rcf) { return !(lcf > rcf); } //等于重载 bool operator==(const CFraction &lcf, const CFraction &rcf) { return lcf.nume == rcf.nume && lcf.deno == rcf.deno; } //不等于重载 bool operator!=(const CFraction &lcf, const CFraction &rcf) { return !(lcf == rcf); } // bool operator>=(const CFraction &lcf, const CFraction &rcf) { if (lcf < rcf) return false; return true; } // bool operator<=(const CFraction &lcf, const CFraction &rcf) { if (lcf > rcf) return false; return true; } int main() { CFraction cf1; CFraction cf2; cin >> cf1 >> cf2; cout << "cf1: " << cf1 << '\t' << "cf2: " << cf2 << endl << "cf1 + cf2 : " << cf1 + cf2 << endl << "cf1 - cf2 : " << cf1 - cf2 << endl << "cf1 * cf2 : " << cf1*cf2 << endl << "cf1 / cf2 : " << cf1 / cf2 << endl << " +cf1 : " << +cf1 << endl << " -cf1 : " << -cf1 << endl << " +cf2 : " << +cf2 << endl << " -cf2 : " << -cf2 << endl << " cf1 > cf2? 1/YES 0/NO " << (cf1 > cf2) << endl << " cf1 < cf2? 1/YES 0/NO " << (cf1 < cf2) << endl << " cf1 == cf2? 1/YES 0/NO " << (cf1 == cf2) << endl << " cf1 != cf2? 1/YES 0/NO " << (cf1 != cf2) << endl << " cf1 >= cf2? 1/YES 0/NO " << (cf1 >= cf2) << endl << " cf1 <= cf2? 1/YES"; cin >> cf2; return 0; }