Advertisement
Vladislav_Bezruk

Untitled

Oct 11th, 2021
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. /*
  2. Створити клас Bool - логічні змінні.
  3. Визначити оператори "+" - логічне АБО, "*" - логічне І " ^ " - ВИКЛЮЧНИМ АБО, як методи класу, а оператор  "!=" як дружню функції.
  4. Оператори повинні дозволяти здійснення операції як з змінними даного класу, так і з змінними вбудованого int.
  5. ( Якщо ціле число відмінне від нуля, вважається що змінна істинна, в іншому випадку помилкова ).
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. class Bool {
  13.     bool value;
  14.    
  15.     public:
  16.         Bool() {
  17.             value = false;
  18.         }
  19.    
  20.         Bool (bool x) : value(x) { 
  21.         }
  22.            
  23.         Bool operator + (const Bool &a) {
  24.             Bool c(value || a.value);
  25.             return c;
  26.         }
  27.        
  28.         Bool operator + (const int a) {
  29.             Bool c(value || a);
  30.             return c;
  31.         }
  32.        
  33.         Bool operator * (const Bool &a) {
  34.             Bool c(value && a.value);
  35.             return c;
  36.         }
  37.        
  38.         Bool operator * (const int a) {
  39.             Bool c(value && a);
  40.             return c;
  41.         }
  42.        
  43.         Bool operator ^ (const Bool &a) {
  44.             Bool c(value ^ a.value);
  45.             return c;
  46.         }
  47.        
  48.         Bool operator ^ (const int a) {
  49.             Bool c(value ^ (a != 0));
  50.             return c;
  51.         }
  52.        
  53.         friend bool operator != (const Bool &a, const Bool &b);
  54.        
  55.         friend bool operator != (const Bool &a, const int b);
  56.        
  57.         void set(char q) {
  58.             cout << "Enter Bool " << q << ": ";
  59.             cin >> value;
  60.         }
  61.        
  62.         void get(char q) {
  63.             cout << "Bool " << q << " = " << value << endl;
  64.         }
  65. };
  66.  
  67. bool operator != (const Bool &a, const Bool &b) {
  68.     return a.value != b.value;
  69. }
  70.  
  71. bool operator != (const Bool &a, const int b) {
  72.     return a.value != (b != 0);
  73. }
  74.  
  75. int main() {
  76.    
  77.     Bool a, b, c;
  78.     int k;
  79.    
  80.     a.set('a');
  81.     b.set('b');
  82.    
  83.     cout << "Enter k: ";
  84.     cin >> k;
  85.    
  86.     cout << "*result*" << endl;
  87.    
  88.     c = a + b;
  89.     cout << "a + b = c, ";
  90.     c.get('c');
  91.    
  92.     c = a + k;
  93.     cout << "a + k = c, ";
  94.     c.get('c');
  95.    
  96.     c = a * b;
  97.     cout << "a * b = c, ";
  98.     c.get('c');
  99.    
  100.     c = a * k;
  101.     cout << "a * k = c, ";
  102.     c.get('c');
  103.    
  104.     c = a ^ b;
  105.     cout << "a ^ b = c, ";
  106.     c.get('c');
  107.    
  108.     c = a ^ k;
  109.     cout << "a ^ k = c, ";
  110.     c.get('c');
  111.    
  112.     cout << "a != b: " << (a != b) << endl;
  113.     cout << "a != k: " << (a != k) << endl;
  114.    
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement