c++练习-类对象对比大小

来源:互联网 发布:手机淘宝买家隐藏评价 编辑:程序博客网 时间:2024/05/21 18:41

0x00 用到的知识点

类的封装、有參构造函数、引用、 this指针


0x01 代码

class Number{public:Number(){}public:Number(const int& iValue) {this->m_Value = iValue;}public:/*两个数相比较大小const Number& obj    比较数实例  返回值:-1 比比较数小0 相等1 比比较数大*/int CompareNumber(const Number& obj){int retValue;if(this->m_Value > obj.m_Value){ retValue = 1;}else if (this->m_Value < obj.m_Value){retValue = -1;}else if (this->m_Value == obj.m_Value){retValue = 0;}return retValue;}private:int m_Value;}; int main(){Number intValueA(10);Number intValueB(20);auto iRes = intValueA.CompareNumber(intValueB);cout << iRes << endl;return 0;}


原创粉丝点击