473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with a truth table

Hello Experts!

I have the user defined class called Boolean that should be handle all kind
of logic expression such
as if (a && b && c) or if (!a && b && c) osv

I have the class definition Boolean and the main program below.
I have overloaded these operator symbols =, ==, !=, !, ||, &&.

Now to my question I have some problem with this Class Boolean that I use as
a trush table.

First this kind of expression if (!b && c) {} gives this compile error
"c:\Documen ts and Settings\Tony\k au\cplusplus\te st10\start.cpp( 23): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"
This statement if (b && !c) {} cause this compile error
"c:\Documen ts and Settings\Tony\k au\cplusplus\te st10\start.cpp( 26): error
C2679: binary '&&' : no operator found which takes a right-hand operand of
type 'bool' (or there is no acceptable conversion)"

As you can see the problem depends on that !b doesnt return an object of
class Boolean but a type of bool.
This expression cause .if (b && c && a) this compile error
"c:\Documen ts and Settings\Tony\k au\cplusplus\te st10\start.cpp( 29): error
C2677: binary '&&' : no global operator found which takes type 'Boolean' (or
there is no acceptable conversion)"

So what should I return from these overloaded operators.

class Boolean
{
public:
enum TruthValue{FALS E, TRUE};
Boolean(TruthVa lue tv)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_val ue);
}
Boolean(const Boolean& src)
{
truth_value = new TruthValue;
*truth_value = *(src.truth_val ue);
}
virtual ~Boolean()
{
delete truth_value;
truth_value = 0;
}
TruthValue getValue()
{ return *truth_value; }

Boolean& operator=(const Boolean&);
bool operator==(cons t Boolean&);
bool operator!=(cons t Boolean&);
bool operator!();
bool operator&&(cons t Boolean&);
bool operator||(cons t Boolean&);
private:
TruthValue* truth_value;
};
bool Boolean::operat or||(const Boolean& h)
{ return *truth_value || *(h.truth_value ); }

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }

bool Boolean::operat or!()
{ return !(*truth_value) ; }

bool Boolean::operat or==(const Boolean& h)
{ return *truth_value == *(h.truth_value ); }

bool Boolean::operat or!=(const Boolean& h)
{ return !(*this == h); }

Boolean& Boolean::operat or=(const Boolean& h)
{
if (this != &h)
{
delete truth_value;
truth_value = new TruthValue;
*truth_value = *(h.truth_value );
}
return *this;
}

#include <iostream>
#include "boolean.h"
using namespace std;
int main()
{
Boolean a(Boolean::TRUE );
Boolean b(Boolean::FALS E);
a=b;

a = Boolean::TRUE;
Boolean c = a;
if (a!= b) cout << "correct";
if (!b) cout << "not correct";
if (b && c) cout << "correct";
if (!b && c) cout << "correct";
if (b && !c) cout << "not correct";

if (b && c && a) cout << "not correct";
return 0;
}

Many thanks

//Tony
Aug 18 '05 #1
6 2185
Tony Johansson wrote:
bool Boolean::operat or||(const Boolean& h)
{ return *truth_value || *(h.truth_value ); }

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.

Marc
Aug 18 '05 #2
Marc Mutz wrote:
bool Boolean::operat or||(const Boolean& h)
{ return *truth_value || *(h.truth_value ); }

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.


Sorry. The problem is that you return bool, but don't have
an implicit conversion from bool to Boolean defined. So
the first && takes the two Boolean, return a bool and
then the second && has a bool as the left-hand argument,
and a Boolean on the right-hand side. Since there's
neither a Boolean->bool nor a bool->Boolean conversion
defined in Boolean, the call can't be satified.

Fix: return Boolean instead of bool from operator&& and
||.

Marc
Aug 18 '05 #3
Tony Johansson wrote:
[lots of snips]
I have the user defined class called Boolean that should be handle all kind
of logic expression such
as if (a && b && c) or if (!a && b && c) osv


I think if you are going to create a Boolean class, then you
have to not use such things as

if (!b) cout << "not correct";

that is, you have to include the explicit full test.

if ((!b) == Boolean(Boolean ::TRUE) ) cout << "not correct";

Or whatever. You want to have !b mean the Boolean
that has the opposite truth value, it has to return
a Boolean. Similarly, you will need to have

if ((a && b && c) == Boolean(Boolean ::TRUE))

and so on. And == returns true/false.
Socks

Aug 18 '05 #4

"Marc Mutz" <ma**@klaralvda lens-datakonsult.se> skrev i meddelandet
news:de******** *****@news.t-online.com...
Marc Mutz wrote:
bool Boolean::operat or||(const Boolean& h)
{ return *truth_value || *(h.truth_value ); }

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }


Binary operators should be implemented as non-member
functions, to solicit implicit conversions on the
left-hand argument. It looks as if that's (at least part
of) your problem.


Sorry. The problem is that you return bool, but don't have
an implicit conversion from bool to Boolean defined. So
the first && takes the two Boolean, return a bool and
then the second && has a bool as the left-hand argument,
and a Boolean on the right-hand side. Since there's
neither a Boolean->bool nor a bool->Boolean conversion
defined in Boolean, the call can't be satified.

Fix: return Boolean instead of bool from operator&& and
||.

Marc


I can return a Boolean from operator&& and operator||
but have you any idea how the definition should look like.

Here it's current definition

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }

//Tony
Aug 18 '05 #5
Tony Johansson wrote:

I can return a Boolean from operator&& and operator||
but have you any idea how the definition should look
like.

Here it's current definition

bool Boolean::operat or&&(const Boolean& h)
{ return *truth_value && *(h.truth_value ); }


Boolean operator&&( const Boolean & lhs, const Boolean &
rhs ) {
return Boolean( calculate-result-here );
}

where calculate-result-here could be a table lookup or a
nested ternary operator (?:) invocation. It's your class,
after all. One example could
return Boolean( *lhs.truth_valu e && *rhs.truth_valu e
? Boolean::TRUE : Boolean::FALSE );
which relies on the implicit conversion from enum to bool,
like your original code.

Marc

Aug 18 '05 #6
Wait ONE!!!
Check Scott Meyer's "Effective C++/
More Effective C++" on why
overloading || and && is a BAD
idea. To summarize, since overloading is a function call, and
BOTH args are evaluated, you
CANNOT duplicate the short-circuit
behaviour of these operators.
This will PROBABLY lead to very
subtle and hard-to-find bugs.

Of course, if this is just an exercise --
NEVERMIND.

Ihor Kinal.

Aug 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6593
by: Mario | last post by:
Can some1 help me with the code for a truth table????
1
2017
by: Snick | last post by:
Hello, I've been going through the DB2 application development manuals and have no idea what I'm doing wrong with my UDF. Basically, I created a C++ library that compares 2 strings and returns their similarity as a double. I wanted to use this function inside DB2 so I created the following code: similarity.cpp ---
2
6532
by: rossum | last post by:
I am using Visual C# Express Beta, though I suspect that my question has more to do with .NET than with C# specifically. I have written a form with a Web Browser in it. At the top I have a button bar. What I want to do is to have a button which searches for user-entered text in the HTML document in the Web Browser. When I run my form I can type Ctrl-F and up comes a standard Find dialogue box which does exactly what I want - the...
15
1591
by: Együd Csaba | last post by:
Hi All, I've a problem with the perfprmance of the production environment. I've two db servers. One on my laptop computer (2Ghz, 1GB, WinXP, Cygwin, Postgres 7.3.4) and one on a production server (2GHz, 1GB, Ultra SCSI, RH7.1, Postgres 7.3.2). I run the same dump and the same query on both of the computers. The difference is substantial. The query takes 5 times longer on the production server then on the laptop.
1
1625
by: Rohanrajs82 | last post by:
Hi, Can anybody help me? I have to construct a truth table of 97 columns. I have tester channels from 1 to 97. I have to make a truth table with all the possible combinations. Also, I have some dummy channels in it where I have to put a dummy value. Regards, Rohan.
6
11449
by: ambanks04 | last post by:
ok taking computer science class anybody know what to do i am not computer literate CoSc 111.003 Spring 2008 Assignment 4 1) Each student shall design, develop, code, test and submit an Original computer program which uses a function or functions to display the TRUTH TABLES for the Logical And, the Logical Or, and the Logical Not operators., as defined in Section 5.8, of the text, 5Th/6Th Edition. 2) The program shall: ...
5
2453
by: smittie31 | last post by:
I am having a problem with a border around me html page. The border does not flow thru the whole html page, it cuts off halfway. --> See http://keithborom.com/marlon-sanders CSS STYLESHEET /* CSS for Marlon Sanders site */ html, body {
18
2115
by: LosLobo | last post by:
Greetings all. I know that cascading lists are a common problem and in truth I my initial post here was to request help with my own, but then I figured out the right code. That being said, I have a new challenge which I'm not sure how to go about. Here is my situation: I presently have 2 combo boxes (cboBuilding, cboRoom) and which are properly cascading. That's great, but now I want to be able to pull the RoomDescription for the room that...
5
6592
by: andrea | last post by:
Well I would like to make a little program that given a certain logical expression gives the complete truth table. It's not too difficult in fact, I just have some doubts on how to design it. I thought something like that: class Term:
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6737
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.