- 3
String comparison using operator overloading
By using ( == ) operator
1 Odpowiedź
- 2
// C++ program to compare two Strings that either they are equal or not
// using Operator(==) Overloading
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
// Class to implement operator overloading function for concatenating the strings
class CompareString {
public:
// Classes object of string
char str[25];
// Parameterized Constructor
CompareString(char str1[])
{
// Initialize the string to class object
strcpy(this->str, str1);
}
/* Overloading '==' under a function which returns integer 1/true
if left operand string and right operand string are equal.
(else return 0/false)
*/
int operator==(CompareString s2)
{
if (strcmp(str, s2.str) == 0)
return 1;
else
return 0;
}
};
void compare(CompareString s1, CompareString s2)
{
if (s1 == s2)
cout << s1.str << " is equal to: "
<< s2.str << endl;
else
{
cout << s1.str << " is