运算符重载: 等于和不等于(==、!=)

等于和不等于(==、!=)

//重载==  ==出现在判断语句中
    bool operator==(Person &ob)
    {
        if(strcmp(this->name, ob.name) == 0)
        {
            return true;
        }
        return false;
    }
    //重载!=  !=出现在判断语句中
    bool operator!=(Person &ob)
    {
        if(strcmp(this->name, ob.name) != 0)
        {
            return true;
        }
        return false;
    }

#千锋教育#

void test02()
{
    Person ob1("lucy");
    Person ob2("lucy");
    Person ob3("bob");

    if(ob1 == ob2)
    {
        cout<<"ob1 == ob2"<<endl;
    }
    else
    {
        cout<<"ob1 != ob2"<<endl;
    }

    if(ob1 != ob3)
    {
        cout<<"ob1 != ob3"<<endl;
    }
    else
    {
        cout<<"ob1 == ob3"<<endl;
    }
}

运行结果:
在这里插入图片描述

发布了52 篇原创文章 · 获赞 42 · 访问量 4937

猜你喜欢

转载自blog.csdn.net/weixin_43288201/article/details/105055217