In: Computer Science
#include <iostream>
#include "lib.hpp"
using namespace std;
int main() {
// declare the bool
bool a = true;
bool b= true;
//Print the Conjunction function
cout<<"\n\nConjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(P∧Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;
//Print the Disjunction function
cout<<"\n\nDisjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(PVQ)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< disjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< disjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< disjunction(!a,!b)<<endl;
//Print the ExclusiveOr function
cout<<"\n\nExclusiveOr Truth Table -"<<endl;
cout<< "\nP\tQ\t(P⊕Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< exclusiveOr(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< exclusiveOr(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< exclusiveOr(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< exclusiveOr(!a,!b)<<endl;
//Print the Negation function
cout<<"\n\nNegation Truth Table -"<<endl;
cout<< "\nP\t~P" <<endl;
cout<< !a <<"\t" << negation(!a)<<endl;
cout<< a <<"\t" << negation(a) <<endl;
How can u do this code Using the enum keyword or a C++ class? To create a new type Boolean with the two values F and T defined.
The given program is implemented by using a C++ class as given below:
#include <iostream>
#include "lib.hpp"
using namespace std;
class BooleanFunction
{
// declare the bool
bool a = true;
bool b= true;
public:
//method to display the conjunction table
void conjunction()
{
//Print the Conjunction function
cout<<"\n\nConjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(P∧Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<<
conjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<<
conjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<<
conjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<<
conjunction(!a,!b)<<endl;
}
//method to display the disjunction table
void disjunction()
{
//Print the Disjunction function
cout<<"\n\nDisjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(PVQ)" <<endl;
cout<< a <<"\t"<< b <<"\t"<<
disjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<<
disjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<<
disjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<<
disjunction(!a,!b)<<endl;
}
//method to display the exclusiveOr table
void exclusiveOR()
{
//Print the ExclusiveOr function
cout<<"\n\nExclusiveOr Truth Table -"<<endl;
cout<< "\nP\tQ\t(P⊕Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<<
exclusiveOr(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<<
exclusiveOr(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<<
exclusiveOr(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<<
exclusiveOr(!a,!b)<<endl;
}
//method to display the negation table
void negation()
{
//Print the Negation function
cout<<"\n\nNegation Truth Table -"<<endl;
cout<< "\nP\t~P" <<endl;
cout<< !a <<"\t" <<
negation(!a)<<endl;
cout<< a <<"\t" << negation(a)
<<endl;
}
};
int main()
{
BooleanFunction b; //object creation statement
b.conjunction(); //method calling
b.disjunction(); //method calling
b.exclusiveOR(); //method calling
b.negation(); //method calling
return 0;
}