Question:
how to write a C++ program to overload (==) operator?
2011-05-21 08:47:12 UTC
how to write a C++ program to overload (==) operator?
Four answers:
?
2011-05-21 09:28:13 UTC
Here is The Easiest Example to overload (==) operator.....I Hope it will Help You much......Thanks



#include

using namespace std;



class Overload

{

int x,y;



public:



Overload(){x=0;y=0;}

Overload(int i,int j){x=i;y=j;}

void get_xy(int &i,int &j){i=x;j=y;}

int operator==(Overload ob1);

};

int Overload::operator==(Overload ob1)

{

return x==ob1.x && y==ob1.y;

}



int main()

{

Overload a(10,20),b(20,10),c(10,20);



if(a==b)

cout<<"A Same as B\n";

else

cout<<"A and B is not same\n";



if(a==c)

cout<<"A Same as C\n";

else

cout<<"A and C is not same\n";



if(b==c)

cout<<"b Same as C\n";

else

cout<<"b and C is not same\n";



return 0;

}
oops
2011-05-21 09:21:51 UTC
Generally, like this:



    struct X

    {

        int a;

        double b;

        std::string c;

    };



    bool operator==(const X & lhs, const X & rhs)

    {

        if(lhs.a != rhs.a) return false;

        if(lhs.b != rhs.b) return false;

        if(lhs.c != rhs.c) return false;

        return true;

    }



You can also overload it as a member function if you want:



    struct X

    {

        int a;

        double b;

        std::string c;



        bool operator==(const X & rhs) const

        {

            if(a != rhs.a) return false;

            if(b != rhs.b) return false;

            if(c != rhs.c) return false;

            return true;

        }

    };



Notes:

It doesn't have to return a bool. It can return anything or even nothing(void). But I wouldn't recommend returning anything other than bool, because it gives your class a sensible interface that matches the usage of operator== in built-in types.



The two parameters don't have to be the same type. For example, you might write a string class, and you might write an overload of op== that accepts one of your strings as the first parameter, and a 'const char *` as the second parameter, that way you can do things like this:



    MyStringClass str;

    if(str == "hello") blah();



However, this is unnecessary if your class has a conversion constructor from that other type. That's exactly how the standard library string class does it.
2011-05-21 08:57:25 UTC
if you mean function overloading then its a program in which there are 2 or more same functions with different prototypes... like



student ( int a , char b [ ] )



and another fuction like



student ( int a, int b )



the program will work...
?
2016-11-19 02:28:26 UTC
i might recommend here header class matrix { public: void operator=(const matrix& proper)//some prefer to declare this matrix& operator=(const matrix&) { /*...*///do your matrix project here }; matrix (const matrix& proper) { (*this)=proper; }; matrix operator* (const matrix& proper) const { matrix effect; /*write the multiplication code here to assign the ultimate effect to ingredient effect*/ return effect; }; }; something of the small print you ought to have the skill to discern on your man or woman. clarification of the code: matrix a,b,c; a=b*c; This code will do here. it is going to call b.operator*(c). on the time it reaches "return effect;" it is going to create a sparkling hidden (named by the compiler) ingredient of sophistication matrix by ability of the constructor matrix( const matrix& proper), with argument the variable effect. enable us to call the invisible ingredient m. finally, the code will invoke a.operator=(m). A be conscious: with the gcc compiler, you might have here optimization: declare the reproduction constructor matrix( const matrix& proper); to be private and supply it no physique. Then gcc won't create one extra invisible merchandise m, and could call no reproduction constructor; quite, it is going to call at as quickly as a.operator=(effect), till now calling the destructor of the object effect. be conscious: this optimization is larger computationally, yet will fail to collect on the microsoft compiler.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...