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

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

//重载==  ==出现在判断语句中
    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;
    }
}

运行结果:
运算符重载: 等于和不等于(==、!=)_第1张图片

你可能感兴趣的:(C++,c++)