• c++第九章-(运算符重载)


    一些规则

    1.c++不允许用户自己定义新的运算符,只能对已有的c++运算符进行重载。

    2.除了五个运算符不允许重载外,其他运算符允许重载:

    • .成员访问运算符
    • *成员指针访问运算符
    • ::与运算符
    • sizeof尺寸运算符
    • ?:条件运算符

    3.重载运算符必须和用户定义的自定义类型的对象一起使用。(也就是说,参数不能全部都是c++的标准类型,这样约定是为了防止用户修改用于标准类型结构的运算符性质)

    4.为什么运算符重载函数有两个参数,只需有一个参数?

    其实是有一个参数是隐含着的,运算符函数是用this指针隐式地访问类对象的成员。

    5.参数传值分三种,值传递、地址传递和引用传值。

    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)
    ";
    }
    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;
    }

    控制台返回的结果是:

    c1 = (3,4i)
    c2 = (5,-10i)
    c1 + c2 = (8,-6i)

     2.重载加减乘除操作符,实现有理数运算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 << "
    ";
        
        res = f1 - f2;
        f1.print();
        std::cout << " - ";
        f2.print();
        std::cout << " = ";
        res.print();
        std::cout << "
    ";
        
        res = f1 * f2;
        f1.print();
        std::cout << " * ";
        f2.print();
        std::cout << " = ";
        res.print();
        std::cout << "
    ";
        
        res = f1 / f2;
        f1.print();
        std::cout << " / ";
        f2.print();
        std::cout << " = ";
        res.print();
        std::cout << "
    ";
        return 0;
    }

     控制台返回的结果:

    1/8 + 7/8 = 1
    1/8 - 7/8 = -3/4
    1/8 * 7/8 = 7/64
    1/8 / 7/8 = 1/7

    3.重载<<操作符

    事实上,我们没法再现有的ostream类专门添加一个新的operator<<()方法。

    所以只能够定义一个正常的函数再外部重载这个操作符,这与重载方法的语法大同小异,唯一的区别是不再有一个对象可以用来调用<<重载函数,而不得不通过第一个输入参数向这个重载方法传递对象。

    operator<<()函数原型,std::ostream&operator<<(std::ostream &os,Ratinoal f),第一个输入参数os是将要向它写数据的那个流,他是以“引用传递”方式传递的。第二个输入参数是打算写到那个流里的数据值,不同的operator<<()重载函数就是因为这个输入参数才相互区别的。

    #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;
        
        friend std::ostream &operator << (std::ostream &os,Rational f);//用于访问私有变量
    };
    
    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 << "
    ";
        
        res = f1 - f2;
        f1.print();
        std::cout << " - ";
        f2.print();
        std::cout << " = ";
        res.print();
        std::cout << "
    ";
        
        res = f1 * f2;
        f1.print();
        std::cout << " * ";
        f2.print();
        std::cout << " = ";
        res.print();
        std::cout << "
    ";
        
        std::cout << f1 << " / " << f2 << " = " << (f1/f2) <<  "
    ";
        return 0;
    }
    
    std::ostream &operator << (std::ostream &os,Rational f);
    std::ostream &operator << (std::ostream &os,Rational f)
    {
        os << f.numerator << "/" << f.denominator;
        return os;
    }
  • 相关阅读:
    求Computational problems associated with Racah algebra
    病理情形
    扫描算法求最大子序列的一次简单非严格证明
    分治法求最大子序列,关于复杂度的一次弱推导
    VFP_等待 rar 命令结束
    Windows_解决win7开机画面变成vista画面的方法
    C#_WinForm的等待窗口
    C#_控件的缩写大全
    SQL Server 2008_基本安装说明
    C#_获取 SQL服务器列表
  • 原文地址:https://www.cnblogs.com/huen/p/3831018.html
Copyright © 2020-2023  润新知