C++运算符重载(三)

来源:互联网 发布:自学编程成才的人 编辑:程序博客网 时间:2024/06/08 19:49

       C++允许在自己的类中,或是在全局作用域中重定义运算符的含义。由于很多面向对象的语言没有提供这种能力,因此你可能会低估这种特性在C++中的作用。C++中运算符的概念十分广泛,甚至包含[](数组索引)、()(函数调用)、类型转换以及内存分配和释放例程。可以通过运算符重载来改变语言运算符对自定义类的行为。能让自己的类具有内建类型的类似行为,甚至可以编写看上去类似于数组、函数或指针的类。在博主的《C++运算符重载》系列博文中会对我们常用的运算符提供重载的实例,希望大家能有所收获。额,本篇博文就让我们一起来探讨一下重载运算符中的算术运算符重载吧。

       算术运算符包括双目运算符“+“(加法)、“-”(减法)、“*”(乘法)、“/”(除法)、“%”(求模)和单目运算符“+”(正号)、“-”(负号)、“++”(前置++和后置++)、“--”(前置—和后置--)。对于双目运算符我们一般把他们重载为全局函数,而单目运算符我们重载为类方法。咱们举个简单的栗子吧。

//Demo.h

#pragma once

 

//定义模板类

template <typenameT>

classDemo

{

public:

    Demo(constT& value =T());

    virtual~Demo(){}

 

    voidshow() const;

 

    template<typename T>/*不能省略*/

    friendT operator + (constT &lhs, constT &rhs);

    template<typename T>/*不能省略*/

    friendT operator - (constT &lhs, constT &rhs);

    template<typename T>/*不能省略*/

    friendT operator * (constT &lhs, constT &rhs);

    template<typename T>/*不能省略*/

    friendT operator / (constT &lhs, constT &rhs);

    template<typename T>/*不能省略*/

    friendT operator % (constT &lhs, constT &rhs);

   

    //负号算术运算符

    constDemo<T>operator - ()const;

    //正号算术运算符

    constDemo<T>operator + ()const;

    //前置++算术运算符

    Demo<T>&operator ++ ();

    //后置++算术运算符

    Demo<T>operator ++ (int);

    //前置--算术运算符

    Demo<T>&operator -- ();

    //后置--算术运算符

    Demo<T>operator -- (int);

 

private:

    T   m_value;

};

 

#include“Demo.inl”

 

//Demo.inl

template <typenameT>

Demo<T>::Demo(constT&value/* =T()*/)

    : m_value(value)

{

 

}

 

template <typenameT>

voidDemo<T>::show()const

{

    cout<< m_value << endl;

}

 

template <typenameT>

constDemo<T>Demo<T>::operator - ()const

{

    returnDemo<T>(-this->m_value);

}

 

template <typenameT>

constDemo<T>Demo<T>::operator + ()const

{

    return*this;

}

 

template <typenameT>

Demo<T>&Demo<T>::operator++ ()

{

    ++(this->m_value);

 

    return*this;

}

 

template <typenameT>

Demo<T>Demo<T>::operator++ (int)

{

    Demo<T>demo(*this);

 

    ++(this->m_value);

 

    returndemo;

}

 

template <typenameT>

Demo<T>&Demo<T>::operator-- ()

{

    --(this->m_value);

 

    return*this;

}

 

template <typenameT>

Demo<T>Demo<T>::operator-- (int)

{

    Demo<T>demo(*this);

 

    --(this->m_value);

 

    returndemo;

}

 

template <typenameT>

T operator + (constT &lhs,constT &rhs)

{

    returnT(lhs.m_value+rhs.m_value);

}

 

 

template <typenameT>

T operator - (constT &lhs,constT &rhs)

{

    returnT(lhs.m_value-rhs.m_value);

}

 

template <typenameT>

T operator * (constT &lhs,constT &rhs)

{

    returnT(lhs.m_value*rhs.m_value);

}

template <typenameT>

T operator / (constT &lhs,constT &rhs)

{

    returnT(lhs.m_value/rhs.m_value);

}

 

template <typenameT>

T operator % (constT &lhs,constT &rhs)

{

    returnT(lhs.m_value%rhs.m_value);

}

 

//main.cpp

#include<iostream>

#include“Demo.h”

usingnamespacestd;

 

int main()

{

    Demo<int>myDemo1(3);

    Demo<int>myDemo2(4);

 

   

    cout<<"myDemo1 + myDemo2: ";

    (myDemo1 +myDemo2).show();

 

    cout<<"myDemo1 - myDemo2: ";

    (myDemo1 -myDemo2).show();

 

    cout<<"myDemo1 * myDemo2: ";

    (myDemo1 *myDemo2).show();

 

    cout<<"myDemo1 / myDemo2: ";

    (myDemo1 /myDemo2).show();

 

    cout<<"myDemo1 % myDemo2: ";

    (myDemo1 %myDemo2).show();

 

    cout<<"-myDemo1: ";

    (-myDemo1).show();

 

    cout<<"+myDemo1: ";

    (+myDemo1).show();

 

    cout<<"++myDemo1: ";

    (++myDemo1).show();

 

    cout<<"myDemo1++: ";

    (myDemo1++).show();

 

    cout<<"--myDemo1: ";

    (--myDemo1).show();

 

    cout<<"myDemo1--: ";

    (myDemo1--).show();

 

    return0;

}

 

程序运行结果:


       是不是感觉使用自定义类Demo时,就像在使用内建类型一样,这也是我们在《C++运算符重载(一)》中说的运算符重载的理由之一:让自定义类的行为和内建类型一样。相信你已经掌握了算术运算符重载函数的编写,是不是很neasy!

        细心的小伙伴可能已经注意到那个“++”(前置++和后置++)和“--”(前置—和后置--)算术运算符,它们做前置和后置的区别就在于有没有参数。其实作为后置运算符时,那个参数没有实际意义,只是做个占位用于区分前置运算符罢了。

        C++运算符重载中的算术运算符重载就讲到这里,相信大家对它的概念和用法都已经熟悉了吧。如果想了解更多关于C++运算符重载的知识,请关注博主的《C++运算符重载》系列博文,在那里我们将会通过程序实例去探讨C++运算符重载的魅力,相信你在那里会有不一样的收获。当然,如果你对C++很感兴趣的话,那么请关注博主的《漫谈继承技术》和《灵活而奇特的C++语言特性》系列博文,在那里你也许会发现C++更大的魅力,让你对这门博大精深的语言更加爱不释手。


1 0
原创粉丝点击