Overload ==, !=, || for MyClass : Boolean Operator « Overload « C++






Overload ==, !=, || for MyClass

Overload ==, !=, || for MyClass
#include <iostream>
using namespace std;

class MyClass {
  int x, y, z;
public:
  MyClass(int i, int j, int k)
  {
    x = i; y = j; z = k;
  }
  MyClass() { x=0; y=0; z=0; }
  void get(int &i, int &j, int &k)
  {
     i = x; j = y; k = z;
  }
  int operator==(MyClass object2);
  int operator!=(MyClass object2); 
  int operator||(MyClass object2); 
 };

int MyClass::operator==(MyClass object2)
{
  return x==object2.x && y==object2.y && z==object2.z;
}

int MyClass::operator!=(MyClass object2)
{
  return x!=object2.x && y!=object2.y && z!=object2.z;
}

int MyClass::operator||(MyClass object2)
{
  return x||object2.x && y||object2.y && z||object2.z;
}

int main()
{
  MyClass object1(10, 10, 10), object2(2, 3, 4), object3(0, 0, 0);

  if(object1==object1) cout << "object1 == object1\n";

  if(object1!=object2) cout << "object1 != object2\n";

  if(object3 || object1) cout << "object1 or object3 is true\n";

  return 0;
}

           
       








Related examples in the same category

1.Array class for 10 int value with overloaded functions: +, -, ==Array class for 10 int value with overloaded functions: +, -, ==