等于和不等于(==、!=)
//重载== ==出现在判断语句中
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;
}
}
运行结果: