+ 6
Looking for a simple way to compare two objects of the same class .
Include"iostream" Using nsmespace std; Class car{ Int price; String color; Public: Void setData (int p,string c){ Price=p; Color=c; Show(); } Void show(){ Cout<<"car info; Cout<<"price"<<price; Cout<<<<"color"<<color; } }; main(){ car obj1,obj2; Obj1.setData(5000,"blue"); Obj2.setData(4000,"blue"); }
3 Answers
+ 3
compare as in check if variables of the objects are same?
you could overload the == operator to return true when all variables of the both classes are same. That is add this function to the car class, i think you need to add this to the public section
bool operator==(car &otherCar)
{
if(price == otherCar.price && color == otherCar.color)
{
return true;
}
return false;
}
this checks if the variables of this and another car objecr are equal
now you can compare obj1 and obj2 with == operator as you would with any other datatype, like int
0
you can get strings of objects that was product from show() and you can compare them
0
caner but show() is a void, it does not return anything