JK Flip Flop Fun

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TMS
    New Member
    • Sep 2006
    • 119

    JK Flip Flop Fun

    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:
    Code:
    #ifndef JKflipFlop_h
    #define JKflipFlop_h
    #include <iostream>
    class JKFlipFlop
    {
    public:
    JKFlipFlop();
    JKFlipFlop(int, int, int, int, int);
    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;
    };
    #endif
     
    #include "JKflipFlop.h"
    #include <iostream>
    using namespace std;
     
    JKFlipFlop::JKFlipFlop()
    {
    }
    /*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    	 constructor for JKfipFlop()
    make a copy of J, K and Q for manipulation.
    ipJ = J, ipK = K, ipQ= Q, ipWire = inputWire
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    */
    JKFlipFlop::JKFlipFlop(int J, int K, int Q, int inputWireA, int inputWireB)
    {
    setJandK(J, K, Q, inputWireA, inputWireB);
    }
     
    /*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    	 function to set ipK, ipK and ipQ
    make a copy of J,K and Q for manipulation.
    ipJ = J, ipK = K, ipQ = Q, ipWire = inputWire
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    */ 
    void JKFlipFlop::setJandK(int J, int K, int Q, int inputWireA, int inputWireB)
    {
    ipJ = J;
    ipK = K;
    ipQ = Q;
    ipWireJ = inputWireA;
    ipWireK = inputWireB;
    }
    /*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    	 Enable JK is called to start the flipFlop function.
    If the inputWire is low, JKFlipFlop is not called and Q retains the ame state.
    If the inputWire is High, JKFlipFlop is called and the wires will be processed.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    */
    int JKFlipFlop::enableJK(int ipWireA, int ipJ, int ipK, int ipQ)
    {
    if(ipWireA == 0)
    {
     
    return ipQ;
    }
    else
    {
     
    return JKFlip(ipWireJ, ipWireK, ipQ);
    }
     
    }
    /*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    	 JK flip:
    -if enable is low, Q or Q' will not be changed, and therefore
    the JKFlipFlop function will not be called.
    -if enable is high && J and K are low Q and Q' will not be changed.
    -					 J is low, K is high Q is set low and Q' is high
    -		J is high and K is low, Q is set to high, Q' is low
    -					 J is high and K is high, determine the value of Q and 
    							make it opposite.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    */
    int JKFlipFlop::JKFlip(int ipJ, int ipK, int ipQ)
    {
    cout << "ipJ = " << ipJ;
    cout << ", ipK = " << ipK ;
    cout << ", ipQ = " << ipQ << endl;
    cout << endl;
    if (ipJ == 0 && ipK == 0)
    {
     
    return ipQ; //ipQ is unchanged
    }
    if (ipJ == 0 && ipK == 1)
    {
    ipQ = 0; //ipQ is reset
    cout << "In J low K high" << ipQ << endl;
     
    }
    if (ipJ == 1 && ipK == 0)
    {
    ipQ = 1;
     
    }
    if (ipJ == 1 && ipK ==1)
    {
    if(ipQ == 0)
    {
    ipQ = 1;
    }
    else
    {
    ipQ = 0;
    }
    }
    return ipQ;
     
    }
     
    JKFlipFlop::~JKFlipFlop()
    {
     
    }
     
     
    };
    #endif
     
     
    #include "JKFlipFlop.h"
    #include <iostream>
    using namespace std;
    int main()
    {
    JKFlipFlop FJK;
    int ipWireA = 0;
    int ipWireB = 0;
    int Jtest = 0;
    int Ktest = 0;
    int Qtest = 0;
    int outputWire = 0;
     
     
    ipWireA = 0;
    Jtest = 0;
    Ktest = 0;
    Qtest = 0;
    outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
    cout << "Result of JK (ipWire 0, J low, K low): " << outputWire << endl;
     
    cout << endl;
    ipWireA = 1;
    Jtest = 0;
    Ktest = 0;
    outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
    cout << "Result of JK (ipWire 1, J low, K low): " << outputWire << endl;
     
    cout << endl;
    ipWireA = 1;
    Jtest = 0;
    Ktest = 1;
    outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
    cout << "Result of JK (ipWire 1, J low, K high): " << outputWire << endl;
     
    cout << endl;
    ipWireA = 1;
    Jtest = 1;
    Ktest = 0;
    outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
    cout << "Result of JK (ipWire 1, J high, K low): " << outputWire << endl;
     
    cout << endl;
    ipWireA = 1;
    Jtest = 1;
    Ktest = 1;
    outputWire = FJK.enableJK(ipWireA, Jtest, Ktest, Qtest);
    cout << "Result of JK (ipWire 1, J = 1, K = 1): " << outputWire << endl;
     
    return 0;
    }
    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your default constructor has no code. Therefore, when you create an object, the member variables are garbage:

    [code=cpp]
    114. JKFlipFlop FJK; //garbage member variables
    [/code]

    There's nothing wrong with how you are passing your function arguments.

    Comment

    • TMS
      New Member
      • Sep 2006
      • 119

      #3
      ok, but when I initialize (for example)

      Code:
       
      JKFlipFlop::JKFlipFlop()
      {
      ipJ = 0; 
      }
      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 ?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by TMS
        JKFlipFlop::JKF lipFlop()
        {
        ipJ = 0;
        }
        OK fine. But you are not initializing all of the class members. You have to do them all.

        Then:
        Originally posted by TMS
        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.

        Comment

        • TMS
          New Member
          • Sep 2006
          • 119

          #5
          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... :(

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I did this:

            [code=cpp]
            JKFlipFlop::JKF lipFlop()
            {
            this->ipJ = 0;
            this->ipK = 0;
            this->ipQ = 0;
            this->ipWireJ = 0;
            this->ipWireK = 0;
            }
            [/code]

            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;
            };

            Comment

            Working...