Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Створити клас Bool - логічні змінні.
- Визначити оператори "+" - логічне АБО, "*" - логічне І " ^ " - ВИКЛЮЧНИМ АБО, як методи класу, а оператор "!=" як дружню функції.
- Оператори повинні дозволяти здійснення операції як з змінними даного класу, так і з змінними вбудованого int.
- ( Якщо ціле число відмінне від нуля, вважається що змінна істинна, в іншому випадку помилкова ).
- */
- #include <iostream>
- using namespace std;
- class Bool {
- bool value;
- public:
- Bool() {
- value = false;
- }
- Bool (bool x) : value(x) {
- }
- Bool operator + (const Bool &a) {
- Bool c(value || a.value);
- return c;
- }
- Bool operator + (const int a) {
- Bool c(value || a);
- return c;
- }
- Bool operator * (const Bool &a) {
- Bool c(value && a.value);
- return c;
- }
- Bool operator * (const int a) {
- Bool c(value && a);
- return c;
- }
- Bool operator ^ (const Bool &a) {
- Bool c(value ^ a.value);
- return c;
- }
- Bool operator ^ (const int a) {
- Bool c(value ^ (a != 0));
- return c;
- }
- friend bool operator != (const Bool &a, const Bool &b);
- friend bool operator != (const Bool &a, const int b);
- void set(char q) {
- cout << "Enter Bool " << q << ": ";
- cin >> value;
- }
- void get(char q) {
- cout << "Bool " << q << " = " << value << endl;
- }
- };
- bool operator != (const Bool &a, const Bool &b) {
- return a.value != b.value;
- }
- bool operator != (const Bool &a, const int b) {
- return a.value != (b != 0);
- }
- int main() {
- Bool a, b, c;
- int k;
- a.set('a');
- b.set('b');
- cout << "Enter k: ";
- cin >> k;
- cout << "*result*" << endl;
- c = a + b;
- cout << "a + b = c, ";
- c.get('c');
- c = a + k;
- cout << "a + k = c, ";
- c.get('c');
- c = a * b;
- cout << "a * b = c, ";
- c.get('c');
- c = a * k;
- cout << "a * k = c, ";
- c.get('c');
- c = a ^ b;
- cout << "a ^ b = c, ";
- c.get('c');
- c = a ^ k;
- cout << "a ^ k = c, ";
- c.get('c');
- cout << "a != b: " << (a != b) << endl;
- cout << "a != k: " << (a != k) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement