How to Overload == Operator in C++? Last Updated : 27 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report A class in C++ is the building block that leads to Object-Oriented programming. Class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. The overloading of operators is a polymorphism that occurs at compile-time. A special meaning can be given to an existing operator in C++ without changing its meaning. Syntax: class sampleClass { public: returntype operator operatoToBeOverloaded ( [arguments required] ) { //sampleCodeHere } }; Except for a few that cannot be overloaded, almost all operators can be overloaded. These operators are as follows: scope resolution operator (::)sizeof operatortypeid operatorThe conditional operator (?:) == is a comparison operator that returns a true or false Boolean value. It checks if the two operands on the operator's left and right sides are equal. Consider a class Car having the following data members: class Class{ private: string name; int cost; }; In this class, we can use the == operator to determine whether the two worker's objects are equivalent. bool operator == (const Car &c) { if (name == c.name && cost == c.cost) return true; return false; } C++ // C++ Program to overload == operator #include<iostream> #include<string> using namespace std; class Car{ private: string name; int cost; public: Car(string n, int c){ name=n; cost=c; } bool operator == (const Car &c){ if (name == c.name && cost == c.cost) return true; return false; } }; int main(){ Car car1 ("Santro",500000); Car car2 ("Safari",1000000); if (car1 == car2) cout<<"Equivalent"<<endl; else cout<<"Not Equivalent"<<endl; } OutputNot Equivalent Comment More infoAdvertise with us Next Article How to Overload == Operator in C++? A anishjawaji28 Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read Types of Operator Overloading in C++ C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks.Syntax: Return_Type classname :: op 4 min read Typecast Operator Overloading in C++ In C++, the typecast operator can be overloaded to customize the behavior of casting operators to define how user-defined data types can be converted into other types. This enables developers to define how instances of a class are converted to other types, providing more control over implicit type c 6 min read C++ Logical (&&, ||, !) Operator Overloading Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi 3 min read Rules for operator overloading In C++, following are the general rules for the things that are not allowed with operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) O 3 min read Like