9.5> 디지털 회로에서 기본적인 게이트로 OR 게이트, AND 게이트, XOR 게이트 등이 있다. 이들은 각각 두 입력 신호를 받아 OR 연산, AND 연산, XOR 연산을 수행한 결과를 출력한다. 이 게이트들을 각각 ORGate, XORGate, ANDGate 클래스로 작성하고자 한다. ORGate, XORGate, ANDGate 클래스가 AbstractGate를 상속받도록 작성하라.
<코드>
#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(); }; bool ANDGate::operation(){return x&y;} //x와 y의 비트and 연산 리턴
class ORGate : public AbstractGate{ public: virtual bool operation(); }; bool ORGate::operation(){return x|y;} //x와 y의 비트or 연산 리턴
class XORGate : public AbstractGate{ public: virtual bool operation(); }; bool XORGate::operation(){return x^y;} //x와 y의 비트xor연산 리턴
int main(void){ 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 return 0; }
|
<결과창>
|
'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글
추상 클래스 상속 (Int Stack) (0) | 2017.12.31 |
---|---|
while, do while loop class (0) | 2017.12.31 |
ForLoopAdder 클래스 (0) | 2017.12.31 |
KmToMile class (0) | 2017.12.31 |
WonToDollar Class (0) | 2017.12.31 |