Answer to Question #283281 in C++ for yash gupta

Question #283281

Write a C++ program which will overload “= =” binary operator. The overload class should

contain one parameterized constructor to initialize default value to the member variable, and a

display function to display value of member variables.


1
Expert's answer
2021-12-28T10:03:38-0500
#include <iostream>

class Integer {
public:
    Integer(int x): var (x) {}

    void Display() const {
        std::cout << var ;
    }

    bool operator==(const Integer& rhs) const {
        return var == rhs.var;
    }
private:
    int var;
};

int main() {
    Integer a(1), b(2), c(1);
    if (a == b) {
        std::cout << "a == b: ";
        a.Display();
        std::cout << " == ";
        b.Display();
        std::cout << '\n';
    }
    else {
        std::cout << "a != b: ";
        a.Display();
        std::cout << " != ";
        b.Display();
        std::cout << '\n';
    }
    if (a == c) {
        std::cout << "a == c: ";
        a.Display();
        std::cout << " == ";
        c.Display();
        std::cout << '\n';
    }
    else {
        std::cout << "a != c: ";
        a.Display();
        std::cout << " != ";
        c.Display();
        std::cout << '\n';
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog