반응형
문제
디지털 회로에서 기본적인 게이트로 OR 게이트, AND 게이트, XOR 게이트 등이 있다.
이들은 각각 두 입력 신호를 받아 OR 연산, AND 연산, XOR 연산을 수행한 결과를 출력한다. 이 게이트들을 각각 ORGate, XORGate, ANDGate 클래스로 작성하고자 한다. ORGate, XORGate, ANDGate 클래스가 AbstractGate를 상속받도록 작성하라.
class AbstractGate { //추상 클래스
protected:
bool x, y;
public:
void set(bool x, bool y) { this->x = x; this->y = y; }
virtual bool operation() = 0;
};
ANDGate, ORGate, XORGate를 활용하는 사례와 결과는 다음과 같다.
int main() {
ANDGate and;
ORGate or;
XORGate xor;
and.set(true, false);
or.set(true, false);
xor.set(true, false);
cout.setf(ios::boolalpha); //불린 값은 "true", "false" 문자열로 출력할 것을 지시
cout << and .operation() << endl; // AND 결과는 false
cout << or.operation() << endl; // OR 결과는 true
cout << xor .opeartion() << endl; // XOR 결과는 true
}
결과
false
true
true
소스코드
#include<iostream>
using namespace std;
class AbstractGate { //추상 클래스
protected:
bool x, y;
public:
void set(bool x, bool y) { this->x = x; this->y = y; }
virtual bool operation() = 0;
};
class ANDGate : public AbstractGate {
public:
virtual bool operation() {
if (x == true && y == true)
return true;
else return false;
}
};
class ORGate : public AbstractGate {
public:
virtual bool operation() {
if (x == true || y == true)
return true;
else return false;
}
};
class XORGate : public AbstractGate {
public:
virtual bool operation() {
if (x != y)
return true;
else return false;
}
};
int main() {
ANDGate And;
ORGate Or;
XORGate Xor;
And.set(true, false);
Or.set(true, false);
Xor.set(true, false);
cout.setf(ios::boolalpha); //불린 값은 "true", "false" 문자열로 출력할 것을 지시
cout << And.operation() << endl; // AND 결과는 false
cout << Or.operation() << endl; // OR 결과는 true
cout << Xor.operation() << endl; // XOR 결과는 true
}
반응형
'명품C++프로그래밍' 카테고리의 다른 글
명품 C++ Programming 실습문제 9장 7, 8번 (0) | 2021.06.01 |
---|---|
명품 C++ Programming 실습문제 9장 6번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 9장 4번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 9장 3번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 9장 2번 (0) | 2021.06.01 |