C++ Program That Defines a Class String and Overload == Operator to Compare Two Strings [Operator Overloading Concept]

Program

#include<iostream>


#include<stdio.h>                             //used
for gets()


#include<string.h>                           //used for strcmp()




using namespace std;




class String


{


        char str[20];




        public:


        void
getdata()                            //function to read the string


        {


               
gets(str);


        }




        //operator
function to overload comparison operator and compare two strings


        int operator
==(String s)


        {


               
if(!strcmp(str,s.str))


                return
1;




                return
0;


        }


};




main()


{


        String s1,s2;


       
cout<<“Enter first string:”;


        s1.getdata();


       
cout<<“Enter second string:”;


        s2.getdata();




       
if(s1==s2)                                    //here
the operator function will be called


        {


               
cout<<“nStrigs are Equaln”;


        }


        else


        {


               
cout<<“nStrings are Not Equaln”;


        }




        return 0;


}

Output

C++ Program That Defines a Class String and Overload == Operator to Compare Two Strings [Operator Overloading Concept]

Clarification:

In this program, we are making a class String and with the assistance of the concept of administrator over-burdening we

are contrasting two strings. The class comprises of an information part str to store string and a capacity getdata() to peruse estimation of str from the client. It additionally contains an administrator

capacity to over-burden == administrator. The administrator work take a class String type an incentive as contention and

thinks about two strings and return 1 when string is equivalent and 0 when they are

inconsistent.

I trust that you can comprehend the code. On the off chance that you have

any uncertainty then you can ask it by remarking underneath. Cheerful Coding!

Leave a Comment

error: Alert: Content is protected!!