#410 – Overloading the == Operator for a Value Type

A user-defined struct automatically inherits an Equals method that performs a value equality check by comparing each field of the struct. The == operator, however, is not automatically defined.  If you want to use the == operator for instances of a struct, you need to overload the == operator.

    public struct PersonHeight
    {
        public int Feet { get; set; }
        public int Inches { get; set; }

        public PersonHeight(int feet, int inches) : this()
        {
            Feet = feet;
            Inches = inches;
        }

        public static bool operator ==(PersonHeight ph1, PersonHeight ph2)
        {
            return (ph1.Feet == ph2.Feet) && (ph1.Inches == ph2.Inches);
        }

        public static bool operator !=(PersonHeight ph1, PersonHeight ph2)
        {
            return !(ph1 == ph2);
        }
    }

Some test cases:

            PersonHeight ph1 = new PersonHeight(5, 10);
            PersonHeight ph2 = new PersonHeight(5, 10);

            // Returns true, default Equals method compares each field
            bool check = ph1.Equals(ph2);

            // == operator also now works - true
            check = (ph1 == ph2);
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #410 – Overloading the == Operator for a Value Type

  1. inez says:

    can you help? I am receiving LOTs of errors….

    #include
    #include
    #include

    using namespace std;

    struct electionList
    {
    void print() const;
    void voteTotal();
    void solvePercentage();
    void calcWinner();
    int votes[100];
    double percentage[100];
    string candidates[2];
    int count;
    int total;

    };

    //CALCULATES TOTAL AMOUNT OF VOTES FOR EACH CANDIDATE
    void electionList::print() const
    {
    cout<<"Candidate"<<"Votes Received"<<"% of Votes"<<endl;

    for (int i=0; i<count; i++)
    {
    cout<<candidates[i];
    cout<<votes[i];
    cout<< percentage[i];
    }

    cout<<"Total"<<total<<endl;
    }

    void electionList::voteTotal()
    {
    int total;
    for (int i=0; i<count; i++)
    {
    total = total + votes[i];
    }
    }

    //CALCULATES PERCENTAGE OF VOTES FOR EACH CANDIDATE

    void electionList::solvePercentage()

    {
    for (int i=0; i<count; i++)
    {
    percentage[i] = ((votes[i]/total)*(100));
    }
    }

    //cOMPARES NUMBER OF VOTES FOR WINNER

    void electionList::calcWinner()
    {
    string winner = candidates[0];
    int max = votes[0];

    for (int i=0; imax)
    {
    max = votes[i];
    winner = candidates[i];
    }
    }

    };

    int main()
    {

    electionList election;
    electionList insert();

    string name;
    int count;

    int tally;
    string candidate[100];
    int votes[100];

    //INPUT DATA

    cout<> count;

    for (int i=0; i<count; i++)

    {
    cout<>name>>tally;
    candidate[i]=name;
    votes[i]=tally;
    //candidate->insert(i,name)>>votes->insert(i, votes);

    }
    count = 1;
    candidate[1]=”thomas”;
    votes[1] = 300;

    cout<<candidate[1]<<votes[1];
    // OUTPUT RESULTS
    cout<<"election result";
    election.print();
    cout<<"election vote total";
    election.voteTotal();};

    • Sean says:

      Did you write this code yourself, or just copy it from somewhere? How are you trying to compile it? It looks like C++ code, so if you’re trying to use a C# compiler to compile it, you’ll certainly get a lot of errors. :O)

  2. inez says:

    I write it but rewrote it and it finally started working…nice site

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: