Computing logic gates. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Computing logic gates.

The input pin of a logic gate has pin1 = string A. pin 2 = string G4O A has the value 1 so thats not a problem. G4O however stands for Gate 4 output. Which means this input pin is conected to Gate 4 output pin. How do I code so that whenever a Gate's input is another Gate's output, it will try to run the previous Gate's output. All gates is their own object as this is an OOP question.

1st Mar 2018, 7:59 AM
Lee Yao Dong
Lee Yao Dong - avatar
1 Answer
+ 2
The gates should have a method to calculate the output. If you set the input for a new gate and want to use the output of the previous gate you set the input with the output method of the previous gate. Assuming you named the methods void setInput(bool in); bool getOutput(); And created the gates named gate1 and gate2 with type GateAND. (just an example) ------------------------- GateAND gate1; GateAND gate2; gate1.setInput(1); //assuming indefinite numbers gate1.setInput(1); //of inputs stored in vector gate2.setInput(1); gate2.setInput(gate1.getOutput()); ------------------------- Now one of the inputs of gate 2 is the output of gate 1. I hope this helps. Here is an example. https://code.sololearn.com/cQ1DLjfjXBkB/?ref=app Note that this just works with 2 inputs in this code. To get multiple inputs working the getOutput method would need to be changed. But it's possible if you create an algorithm for it ^^
1st Mar 2018, 9:05 AM
Alex
Alex - avatar