Back to the basics - How to setup a class
-
Ok, so Im not an expert by far in programing. I know enough to create some basic programs. I try to read books to learn more, but I cant seem to bridge the gap between books and actual. Im posting here for some advice on how to begin constructing a class, what should and should not be in it.
For example. I created a Gear program for work that did a bunch of calculations on the fly when a user made a change in a spin box where all other values dependent on that value got updated. The code is sloppy, and I wanted to have another go at it and make it cleaner. I wanted to create a class that if possible could be reused in something else, as it stands, its so crappy I cant just drop it into a new project and get going.So Im asking for some advice on what maybe should be in the class and what should not be. I wanted to create a gear class that would hold every aspect of a gear I can think of, and when one value is changed, everything else gets updated. Then all I would have to do is call the class for a particular value in my main program and not worry about any calculations in the main prog.
Should I, when a value of the gear is modified, crunch all the numbers, update all the data internally in the class, and then when all done, emit a signal for everything that changed so the form can update? Am I going at this the wrong way? Any guidance would be appreciated.
Thanks .
-
Hi,
Is it possible you can present the code ?
I'm no expert myself but, I would like to see how the
program can work.From what I understand you want to be able to use you
gears for future applications. Once that gears "move" or
change calculations the programs which relies on it also
changes. -
well if you will provide your current code then we can think about but also provide some explanations on the result that are you expecting on each function and also what the gear class suppose to do. as better explained - the result will be better.
-
As it stands, it all widget event driven functions that call other widget events to update data. I have duplicate calculations in multiple functions, and really, I want to scrap the whole thing and start over. Its so awful. I dont want to try and fix it, I want to re-write it all, lol
Imagine a gear, it has 6 main different things.
Number of Teeth,
Outside Diameter,
Pitch Diameter,
Root Diameter.
Diametral Pitch,
Pressure AngleOf these 6 main items, many others thing are calculated.
I want a form that has all these items on it. When one item is changed, all other values in all other items also get adjusted to reflect.As it stands, I have such a cluster mess of inter twined widget event functions, half ass flags to stop execution so i dont get caught in loops, etc. I wanted a more elegant approach to this using classes, but not really sure the best way to structure it.
I am looking for a simple, for example, in my form... SetNumberOfTeeth(), or GetNumberOfTeeth()....everything else should be done behind the scenes.
If I call SetNumberOfTeeth(11), all other data dependent on that value will be automatically adjusted.
My problem is...and I can create just as crappy code inside the class too but I dont want to...is how should a class be designed?
I understand that is very general of a question.
-
this is very easy one is just pure c++ and is not related to Qt.
you have to have the class Gear with all the vars private and with some functions that will do like that
@void SetNumberOfTeeth(int arg1){
this->numberOfTheeth = arg1;
}@@int GetNumberOfTeeth(){
return numberOfTheeth;
}@
and same thing for the
Outside Diameter,
Pitch Diameter,
Root Diameter.
Diametral Pitch,
Pressure Anglethen in the same class you have to have like a
@bool numberOfTeethChanged(int noOfTh){
and here you do your calculation regarding the change of the Teeth
as I've understood you modify all other values
}@and repeat for all other parameters.
in the constructor you can initiate the function with a pre-defined parameter or you can create 7 constructors or one constructor depending on how do you want to make your constructor something like
@Gear::Gear(int NumberofTeeth,
int OutsideDiameter,
int PitchDiameter,
int RootDiameter.
int DiametralPitch,
int PressureAngle){
you set the values
int or double or up to you
}@or something like
@Gear::Gear(QString type, int arg1){
if (type == "Number of Teeth")
numberOfTeethChanged(arg1);
else if(type == "Outside Diameter")
outsideDiameterChanged()arg1;
..........................
and so on
}@as I've already mentioned is pure c++
-
Well I posted here because I was not sure if I should use signals to tell the form changes were made. Now im thinking, one signal to tell the form to update all data. Thanks for the help, I think I will start hacking at it.
-
Well if you want you can use signal and slot future. I'm your window you can do something like @void onMyLineEditGearTeeth(){} @ and connect this to the line edit or with spinbox on signal value changed or text edited