38_等於和不等於(==、!=)運算符重載

//重載==  ==出現在判斷語句中
    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;
    }
}

運行結果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章