C++中重载运算符

来源:互联网 发布:黑莓z10淘宝 编辑:程序博客网 时间:2024/06/14 02:49

重载运算符,可以定义运算符为自己想要的效果,简化程序,以重载<运算符为例:

#ifndef BOX_H#define BOX_Hclass Box{public:Box(double aLength=1.0,double aWidth=1.0,double aHeight=1.0);double volume() const;double getLength() const;double getWidth() const;double getHeight() const;//重载运算符<bool operator < (const Box& aBox) const{return volume()<aBox.volume();}private :double length;double width;double height;};#endif

这样就可以直接调用<运算符直接比较体积。

0 0
原创粉丝点击