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: -
#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
5 9207
Your default constructor has no code. Therefore, when you create an object, the member variables are garbage: -
114. JKFlipFlop FJK; //garbage member variables
-
There's nothing wrong with how you are passing your function arguments.
TMS 119
New Member
ok, but when I initialize (for example) -
-
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 ?
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.
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... :(
I did this: -
JKFlipFlop::JKFlipFlop()
-
{
-
this->ipJ = 0;
-
this->ipK = 0;
-
this->ipQ = 0;
-
this->ipWireJ = 0;
-
this->ipWireK = 0;
-
}
-
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;
};
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: RicercatoreSbadato |
last post by:
I have to flip vertically a Bitmap. Is it possible with GDI+ and C#?
|
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.
|
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
|
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
|
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.
| |
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. ...
|
by: Francesco Moi |
last post by:
Hi.
I'd like to flip horizontally an image with JavaScript. Is it
possible? Thank you very much.
|
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...
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |