Hi Ryan,
First up, a tip for future posts: Use code tags - select your code, press the
<>
button on the format menu. It makes it easier for everyone, there are line numbers, and we can compile the code if a complete program is posted.
http://www.cplusplus.com/articles/z13hAqkS/
rjblair87 wrote: |
---|
The reason I want to change it is because I have read its bad practice to have everything in your main function and would like to learn how to make more professional looking programs. |
Yes that is right, but there is more than one way of doing it.
The first way is to write code with functions, this what happens in lots of languages, obviously in those which don't have classes at all. Functions should only do 1 conceptual thing, and be quite short, including the main function. One could have a program with more than 1 MLOC (Millions of Lines Of Code) with a main function 10 LOC.
The reason I mention this is that sometimes it's not worth it to put code into classes. Even when there are classes, some of the functions can be free functions (functions outside a class). However that doesn't mean that one gravitates into writing C code all the time.
Next is realize the basic concept of a class: Member variables (the data), and functions which operate on that data, are all bundled together into a class. This generally makes it easier to model real world and abstract situations. Sounds easy enough, but OOP does get complicated when there are multiple classes.
Anyway, there is so much to talk about, better start with a tutorial:
http://www.cplusplus.com/doc/tutorial/classes/
Also I have some personal preferences about the code I write, in the following I don't mean to dis
fiji885's style. However I do have reasons why I do these things.
I don't pre-pend member variables with m_ or any other common appends such as an underscore. I prefer to put an Arg on the end of the function parameter identifiers, this separates them from the member variables.
A function declaration doesn't have to have
const
for the parameters, but do put them in in the function definitions;
I put the & for the reference next to the type, because C++ revolves around types;
I always put a variable name in the function declaration, the same as the one in the definition:
1 2
|
public:
store(const std::string& itemNameArg);
|
store::store(const std::string& itemNameArg)
I always declare 1 variable per LOC. Ideally wait until you have a sensible value to assign, then do the declaration and assignment all in one line:
int production1 = ts1 + sd1;
Some good reading:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md