有如下程序: #include<iostream> using namespace std; class Pair{ int m; int n; public: Pair(int i,intj):m(i

admin2020-11-11  15

问题 有如下程序:
   #include<iostream>
    using namespace std;
    class Pair{
    int m;
    int n;
    public:
    Pair(int i,intj):m(i),n(j){}
    bool operator>(Pair P)const;    //须在类体外给出定义
      };
    int main(){
    Pair p1(3,4),p2(4,3),p3(4,5);
   cout<<(p1>p2)<<(p2>p1)<<(p2>p3)<<(p3>p2);
    return 0;
    }
    运算符函数。perator>的功能是比较两个Pair对象的大小当左边对象大时,返回ture,否则返回false。比较规则是首先比较两对象的m成员,m大者为大:当m相等时比较n,n大者为大。程序输出0101,下列对运算符重载函数的正确定义是(    )。

选项 A、bool Pair::operator>(Pair p)const
{if(mt=p.m)return m>p.m;return n>p.n;}
B、bool Pair::operator>(Pair p)
{if(m!=p.m)return m>p.m;return n>p.n;}
C、bool Pair::operator>(Pair p)eonst
{if(m>p.m)return true;return n>p.n;}
D、bool Pair::operator>(Pair p)
{if(m>p.m)return true;return n>p.n;}

答案A

解析 按照比较规则:首先比较两对象的m成员,m大者为大:当m相等时比较n,n大者为大。这条规则的用C++写出来就是选项A。
转载请注明原文地址:https://kaotiyun.com/show/ieyp777K
0

相关试题推荐
最新回复(0)