C++ program to compare two Strings using Operator Overloading
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
My Personal Notes arrow_drop_up
Please Login to comment...