落書きノート

ふと自分が気になった事を書いてます

C++で問題を解く

#ifndef CLASS_BOOLEAN
#define CLASS_BOOLEAN

class Boolean {
public:
  enum boolean {False, True};
private:
  boolean v;
public:
  Boolean() : v(False) {}
  Boolean(const int& n) : v(n == 0 ? False : True) {}

  operator int() const { return v; }
  operator const char*() const { return v == False ? "False" : "True"; }
  bool operator!() const { return v == False; }
};

inline std::ostream& operator<<(std::ostream& s, const Boolean& x){
  return s << static_cast<const char*>(x);
}

#endif
#include <iostream>
#include "q12_2.h"

using namespace std;

Boolean int_eq(int x, int y){
  return x == y;
}

int main(void){
  int n;
  Boolean a(5);
  Boolean b = a;
  Boolean c = 100;
  Boolean x[8];

  cout << "整数値 : ";
  cin >> n;
  x[0] = int_eq(n, 5);
  x[1] = (n != 3);
  x[2] = Boolean::False;
  x[3] = 1000;
  x[4] = c == Boolean::True;
  x[5] = !x[4];

  cout << "aの値 : " << int(a) << endl;
  cout << "aの値 : " << static_cast<int>(a) << endl;
  cout << "bの値 : " << static_cast<const char*>(b) << endl;

  for(int i = 0; i < 8; i++)
    cout << "x[" << i << "] = " << x[i] << endl;
}