Adding operator of boolean class

Pages: 12
Hi,

I thought about an idea that will make my h.w. alot easier.
To do that, I need to add an operator+ of 2 boolean variables.

Is that possible? If possible, how?

Tnx alot,
Maor.
for a binary operator to have two parameters, you will have to define the operator out of the class.
something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class x
{
private:
	int data;

	x()
	{
		data = 0;
	}

	x(int i)
	{
		data = i;
	}

	friend x operator+ (x& x1, x& x2);

};

x operator+ (x& x1, x& x2)
{
	return x(x1.data + x2.data);
}
I need something that will make:
1
2
bool b1=true,b2=false;
b1 = b1+b2 ;


work.
Is that possible ?
Last edited on
@OPSure. Define the + operator like writetonsharmadid, but with boolean variables rather than variables of type class x. NB: For booleans, do not pass the arguments by reference as they are intrinsic types.

EDIT: However, why exactly to you want to add booleans? This seems to me to be a counter intuitive thing to do, as they are not an arithmetic type.

EDIT EDIT: @writetonsharma Shouldn't those parameters be const?
Last edited on

@Xander314, I think so. :)

Edit: I thought the user should also do some research. :)

Edit Edit: @maory, the constructors should be public. My mistake. :P
Last edited on
EDIT: I just tried it, and you can't make a global operator with two intrinsic typed parameters. So it won't work, sorry.
You have check it again. That's a perfect thing to make operator's global. Why it ain't working? here is the code which i check after your last post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class x
{
private:
	int data;

public:

	x()
	{
		data = 0;
	}

	x(int i)
	{
		data = i;
	}

	friend x operator+ (const x& x1, const x& x2);

};

x operator+ (const x& x1, const x& x2)
{
	return x(x1.data + x2.data);
}

int main(int argc, char **argv)
{
	x x1 = 10;
	x x2 = 20;
	x x3 = x1 + x2;
}
@Xander314
Booleans are an algebraic type...

@maory
You can add booleans (which is boolean disjunction), but in C++ it technically means something other than what you expect.
Instead of using +, just use the proper boolean operator: |.

Hope this helps.
Um... Do you want the operators a || b (or), a && b (and), and !a (not)?

Or maybe you wanted:
1
2
bool a, b; //define a and b
int sum = (int)a + (int)b;


What do you want the sum of two booleans to return? For instance, what should true + false equal?
Hi all,

Im building a template class of 'sparse matrix' (implemented by my LinkedList) that has += operator.
When my matrix recieves bool elements, I want the function operator+= be like AND (&&).
this is why I want to create a operator+ for boolean that will solve my problem.

Let me check your solution.

I forgot to mention:

The operator needs to be at the matrix class, but it has no connection to it. Just an operator for 2 boolean variables.

writetonsharma your solution cannot work cuse I cant open class name 'bool'.

mybe there is a solution like creating operator<< with 'ostream'. something like:
1
2
3
	//I/O operator
	friend ostream& operator<<(ostream& , const String&);
	friend istream& operator>>(istream& , String&);


but with bool.

@maory, this was just an example for you to see how a binary operator can take two arguments. How you use this is up to you. :)
I know, and thanks for that :)

But Im asking if it possible to create an operator of bool, int or char. for example:
To create an operator && for chars, or operator + for bool.

soo.. it is possible ?
You can overload any operator in c++ although I am confused as to what you will gain by a+b instead of using the standard operators, unless you want a + (and) b to mean something entirely different that what it usually means.
bool + bool = bool || bool.
I want to overload it, that it will be: bool && bool..
As pointed out by Xander, you cannot overload operators between two intrinsic data types, such as bool and bool. You could bool and SomeClass, or SomeClass and bool, but not bool and bool.
ok. I thought there is a simple way to solve my problem.
My solution is to create new class Bool with all the needed operators.

Thanks alot.
Maor.
I really hate how these boards see more and more people expounding their opinion as fact.

Have you tried using + on booleans? (no)

Booleans are an algebraic type... and they can be treated as such in C++ by using a simple type promotion trick:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iomanip>
#include <iostream>
using namespace std;

int main()
  {
  bool xs[] = { false, false, true,  true };
  bool ys[] = { false, true,  false, true };

  cout << boolalpha
       << setw( 22 ) << "disjunction (or)"
       << "\t"
       << setw( 22 ) << "conjunction (and)\n";

  for (unsigned n = 0; n < 4; n++)
    {
    cout         << setw( 6 ) << xs[ n ]
         << " +" << setw( 6 ) << ys[ n ]
         << " =" << setw( 6 )

         << (bool)(xs[ n ] + ys[ n ]);

    cout << "\t" << setw( 6 ) << xs[ n ]
         << " *" << setw( 6 ) << ys[ n ]
         << " =" << setw( 6 )

         << (bool)(xs[ n ] * ys[ n ]);

    cout << endl;
    }

  return 0;
  }

The trick is there on lines 21 and 27, where the result is cast back from an integer to a boolean.

Hence, your matrix class, so long as you immediately cast algebraic operations back to bool, needs no special handling for booleans over other integer types.

Hope this helps.
I want to overload it [operator+], that it will be: bool && bool..

Except that 0 will be true and 1 false. or find a way that 1+0 == 0
Last edited on
...except that 1+0 == 1

Go study your boolean algebra.
Pages: 12