首页 > 代码库 > c++第九章-(运算符重载)
c++第九章-(运算符重载)
一些规则
1.c++不允许用户自己定义新的运算符,只能对已有的c++运算符进行重载。
2.除了五个运算符不允许重载外,其他运算符允许重载:
- .成员访问运算符
- *成员指针访问运算符
- ::与运算符
- sizeof尺寸运算符
- ?:条件运算符
3.重载运算符必须和用户定义的自定义类型的对象一起使用。(也就是说,参数不能全部都是c++的标准类型,这样约定是为了防止用户修改用于标准类型结构的运算符性质)
4.为什么运算符重载函数有两个参数,只需有一个参数?
其实是有一个参数是隐含着的,运算符函数是用this指针隐式地访问类对象的成员。
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(int argc, const char * argv[]){ 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;}
有理数运算demo
#include <stdlib.h>class Rational{public: Rational(int num,int denom);//num用于分子,denom用于分母 Rational operator+(Rational rhs); 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){ this->numerator = num; this->denominator = denom; normalize();}Rational Rational::operator+(Rational rhs){ int a = numerator; int b = denominator; int c = rhs.numerator; int d = rhs.denominator; int e = a * b + c * d; int f = b * d; return Rational(e,f);}Rational Rational::operator-(Rational rhs){ rhs.numerator = -rhs.numerator; return operator+(rhs);}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);}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; }}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;}int main(int argc, const char * argv[]){ 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"; return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。