473,748 Members | 9,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JK Flip Flop Fun

TMS
119 New Member
I'm writing a couple of classes one for a D Flip Flop and one for a JK Flip Flop in C++. The D Flip Flop seems to be working fine, but I'm having issues with the JK Flip Flop.

I'm getting garbage for the variables when passing from one function to the next. Here is my code for the JK Flip Flop header, class and test:
Expand|Select|Wrap|Line Numbers
  1. #ifndef JKflipFlop_h
  2. #define JKflipFlop_h
  3. #include <iostream>
  4. class JKFlipFlop
  5. {
  6. public:
  7. JKFlipFlop();
  8. JKFlipFlop(int, int, int, int, int);
  9. void setJandK(int, int, int, int, int);
  10. int JKFlip(int ipJ, int ipK, int ipQ);
  11. int enableJK(int inputWire, int ipJ, int ipK, int ipQ);
  12. ~JKFlipFlop();
  13. private:
  14. int ipJ;
  15. int ipK;
  16. int ipQ;
  17. int ipWireJ;
  18. int ipWireK;
  19. };
  20. #endif
  21.  
  22. #include "JKflipFlop.h"
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. JKFlipFlop::JKFlipFlop()
  27. {
  28. }
  29. /*
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31.      constructor for JKfipFlop()
  32. make a copy of J, K and Q for manipulation.
  33. ipJ = J, ipK = K, ipQ= Q, ipWire = inputWire
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. */
  36. JKFlipFlop::JKFlipFlop(int J, int K, int Q, int inputWireA, int inputWireB)
  37. {
  38. setJandK(J, K, Q, inputWireA, inputWireB);
  39. }
  40.  
  41. /*
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43.      function to set ipK, ipK and ipQ
  44. make a copy of J,K and Q for manipulation.
  45. ipJ = J, ipK = K, ipQ = Q, ipWire = inputWire
  46. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. */ 
  48. void JKFlipFlop::setJandK(int J, int K, int Q, int inputWireA, int inputWireB)
  49. {
  50. ipJ = J;
  51. ipK = K;
  52. ipQ = Q;
  53. ipWireJ = inputWireA;
  54. ipWireK = inputWireB;
  55. }
  56. /*
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58.      Enable JK is called to start the flipFlop function.
  59. If the inputWire is low, JKFlipFlop is not called and Q retains the ame state.
  60. If the inputWire is High, JKFlipFlop is called and the wires will be processed.
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. */
  63. int JKFlipFlop::enableJK(int ipWireA, int ipJ, int ipK, int ipQ)
  64. {
  65. if(ipWireA == 0)
  66. {
  67.  
  68. return ipQ;
  69. }
  70. else
  71. {
  72.  
  73. return JKFlip(ipWireJ, ipWireK, ipQ);
  74. }
  75.  
  76. }
  77. /*
  78. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79.      JK flip:
  80. -if enable is low, Q or Q' will not be changed, and therefore
  81. the JKFlipFlop function will not be called.
  82. -if enable is high && J and K are low Q and Q' will not be changed.
  83. -                     J is low, K is high Q is set low and Q' is high
  84. -        J is high and K is low, Q is set to high, Q' is low
  85. -                     J is high and K is high, determine the value of Q and 
  86.                             make it opposite.
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. */
  89. int JKFlipFlop::JKFlip(int ipJ, int ipK, int ipQ)
  90. {
  91. cout << "ipJ = " << ipJ;
  92. cout << ", ipK = " << ipK ;
  93. cout << ", ipQ = " << ipQ << endl;
  94. cout << endl;
  95. if (ipJ == 0 && ipK == 0)
  96. {
  97.  
  98. return ipQ; //ipQ is unchanged
  99. }
  100. if (ipJ == 0 && ipK == 1)
  101. {
  102. ipQ = 0; //ipQ is reset
  103. cout << "In J low K high" << ipQ << endl;
  104.  
  105. }
  106. if (ipJ == 1 && ipK == 0)
  107. {
  108. ipQ = 1;
  109.  
  110. }
  111. if (ipJ == 1 && ipK ==1)
  112. {
  113. if(ipQ == 0)
  114. {
  115. ipQ = 1;
  116. }
  117. else
  118. {
  119. ipQ = 0;
  120. }
  121. }
  122. return ipQ;
  123.  
  124. }
  125.  
  126. JKFlipFlop::~JKFlipFlop()
  127. {
  128.  
  129. }
  130.  
  131.  
  132. };
  133. #endif
  134.  
  135.  
  136. #include "JKFlipFlop.h"
  137. #include <iostream>
  138. using namespace std;
  139. int main()
  140. {
  141. JKFlipFlop FJK;
  142. int ipWireA = 0;
  143. int ipWireB = 0;
  144. int Jtest = 0;
  145. int Ktest = 0;
  146. int Qtest = 0;
  147. int outputWire = 0;
  148.  
  149.  
  150. ipWireA = 0;
  151. Jtest = 0;
  152. Ktest = 0;
  153. Qtest = 0;
  154. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  155. cout << "Result of JK (ipWire 0, J low, K low): " << outputWire << endl;
  156.  
  157. cout << endl;
  158. ipWireA = 1;
  159. Jtest = 0;
  160. Ktest = 0;
  161. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  162. cout << "Result of JK (ipWire 1, J low, K low): " << outputWire << endl;
  163.  
  164. cout << endl;
  165. ipWireA = 1;
  166. Jtest = 0;
  167. Ktest = 1;
  168. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  169. cout << "Result of JK (ipWire 1, J low, K high): " << outputWire << endl;
  170.  
  171. cout << endl;
  172. ipWireA = 1;
  173. Jtest = 1;
  174. Ktest = 0;
  175. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  176. cout << "Result of JK (ipWire 1, J high, K low): " << outputWire << endl;
  177.  
  178. cout << endl;
  179. ipWireA = 1;
  180. Jtest = 1;
  181. Ktest = 1;
  182. outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
  183. cout << "Result of JK (ipWire 1, J = 1, K = 1): " << outputWire << endl;
  184.  
  185. return 0;
  186. }
  187.  
  188.  
It's been a while since I've written anything in C++, so it's probably obvious to the experts out there, maybe even to non-experts, but I can't see it and I'm tired of loosing sleep over it, lol. Perhaps it's just my logic, IDK...

Any help would be appreciated.

TMS
Sep 5 '07 #1
5 9207
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your default constructor has no code. Therefore, when you create an object, the member variables are garbage:

Expand|Select|Wrap|Line Numbers
  1. 114. JKFlipFlop FJK;          //garbage member variables
  2.  
There's nothing wrong with how you are passing your function arguments.
Sep 5 '07 #2
TMS
119 New Member
ok, but when I initialize (for example)

Expand|Select|Wrap|Line Numbers
  1.  
  2. JKFlipFlop::JKFlipFlop()
  3. {
  4. ipJ = 0; 
  5. }
  6.  
it does the same thing (garbage out). So, I'm missing something between that and the copy constructor I use just after this, right? What am I missing ?
Sep 5 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
JKFlipFlop::JKF lipFlop()
{
ipJ = 0;
}
OK fine. But you are not initializing all of the class members. You have to do them all.

Then:
So, I'm missing something between that and the copy constructor I use just after this, right?
You have no copy constructor. That means if you use a JKFlipFlop as a function argument, the compiler will trot out its default copy constructor which will memberwise copy your object. Considering the data members are all int, this should be OK.

However, you aren't calling a function using a JKFlipFlop as an argument so I don't understand what you mean by your using a copy constructor.
Sep 5 '07 #4
TMS
119 New Member
granted... that was an example. I wanted to see if that was correct. So, I tried initializing all the members, like what I showed previously, and it still doesn't work.

Here is the sad part... I thought that the constructor I used to set the values was the copy constructor. I think I have it all backwards but I keep getting in an endless loop of wrong... :(
Sep 5 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
I did this:

Expand|Select|Wrap|Line Numbers
  1. JKFlipFlop::JKFlipFlop()
  2. {
  3.     this->ipJ = 0;
  4.     this->ipK = 0;
  5.     this->ipQ = 0;
  6.     this->ipWireJ = 0;
  7.     this->ipWireK = 0;
  8. }
  9.  
and your values look OK. But I didn't follow your logic. I just verified they were initialized.


A constructor is called by the compiler after an object has been created and after the default constructor on every class memeber has been called. So, byt the time you get to the { of the constructor, your object is conmpletely built and ready to go. The purpose of the constructor is to add initializations not already done. In your case, setting the class data members to 0.

A copy constructor is a constructor that takes a const class reference as an argument. It is used when you need to create a new object from an existing one. After the new object is created and after default constructor on every class memeber has been called, you can then add any initializations not already done. See below.

[code=cpp]
class JKFlipFlop
{
public:
JKFlipFlop();
JKFlipFlop(int, int, int, int, int);
JKFlipFlop(cons t JKFlipFlop& arg); //copy ctor <<<---
void setJandK(int, int, int, int, int);
int JKFlip(int ipJ, int ipK, int ipQ);
int enableJK(int inputWire, int ipJ, int ipK, int ipQ);
~JKFlipFlop();
private:
int ipJ;
int ipK;
int ipQ;
int ipWireJ;
int ipWireK;
};
Sep 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3049
by: RicercatoreSbadato | last post by:
I have to flip vertically a Bitmap. Is it possible with GDI+ and C#?
0
1299
by: Mr. A | last post by:
I'm looking for a code to flip a window, like in the Tiger - Apple OS. I though of the following algorithm but I can get it implemented right: 1. Capture the window who is about to be flipped. 2. Hide it and display the image. This is the part which I don't know how to implement: 3. Start changing the image size so it looks like its being flipped. 4. Display a new window which is the "back side" of the original window.
0
1548
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times. How do I get a computer to flip coin and get result? Any ideas??? Any help will be greatly appreciated greatly!!!!!!!! Andy
8
6761
by: coolindienc | last post by:
At first I had no idea where to start. Finally I tried to get somewhere and I am still stuck. I wrote two different ways using while loop and for loop. Below are codes for both. I am getting errors different places for each loop. For this code I am getting error at "else" statement. import random heads = tails = count = 0 while count <= 100: flip = random.randrange(2) if flip == 0: heads += 1
71
4853
by: Murray R. Van Luyn | last post by:
Hi, Since I have made changes to my website it's been a complete flop. According to the logs, as soon as visitors have downloaded the index page they are off. I can't figure out why? http://www.review-a-gadget.com/ Is there anything obvious that I am missing? Are there problems with some browsers? Please let me know if you notice anything.
7
2806
by: kumana1 | last post by:
Here's my dilema: My database is built around people and their contact info. On my "data entry form" there is a place for their main address, and on the subform which is attached to the main form, is a place for the person's home address. I have a checkbox on the main form which I want to use to swap/flip-flop the home address with the main address. The concept I have figured out for the most part, but the code is the actual problem. ...
6
15745
by: Francesco Moi | last post by:
Hi. I'd like to flip horizontally an image with JavaScript. Is it possible? Thank you very much.
2
10326
by: as | last post by:
Hi folks, am a newbie in C++ world. Normally I use matlab more often. But right now I want to create a Rising Edge D Flip-Flop model which has two Digital inputs, i.e. INPUTDATA and a CLOCK. The functionality of Rising Edge D Flip Flop is that whenever a rising edge of CLOCK signal will occur ( Rising Edge : 0 to 1 ) the output will be the similar state of INPUTDATA signal, otherwise (if it will be not a rising edge and CLOCK signal is 0...
6
6338
by: dissectcode | last post by:
Is there a quick method to flip endian-ness in C? Different platforms I am using, use big and little endian, and I want to flip everything to big.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.