Pre-requisite: Operator Overloading in C++
Given two strings, how to check if the two strings are equal or not, using Operator Overloading.
Input: ABCD, XYZ Output: ABCD is not equal to XYZ ABCD is greater than XYZ Input: Geeks, Geeks Output: Geeks is equal to Geeks
Approach: Using binary operator overloading.
- Declare a class with a string variable and operator function ‘==’, ‘<=’ and ‘>=’ that accepts an instance of the class and compares it’s variable with the string variable of the current instance.
- Create two instances of the class and initialize their class variables with the two input strings respectively.
- Now, use the overloaded operator(==, <= and >=) function to compare the class variable of the two instances.
Below is the implementation of the above approach:
Comparing "Geeks" and "ForGeeks" Geeks is not equal to ForGeeks ForGeeks is greater than Geeks Comparing "Geeks" and "Geeks" Geeks is equal to Geeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live