d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Please.. C++ > Simulation.
Prev12
Add Reply New Topic New Poll
Member
Posts: 18,513
Joined: Dec 17 2005
Gold: 53.50
Apr 9 2014 11:41am
mmm.. I'm trying to learn how you guys are doing it. I haven't learned some of these stuff.
I use using namespace std;, but I can see that you can use std:: instead.
I understand the setters/getters and had it set up like that.
But I don't understand why you use "int main(int argc, const char * argc). In fact, I'm not sure what that does for main and how that would be called.
Not really understanding the typedef short Ushort and typedef const Ushort cUshort.
:////////////
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 9 2014 12:14pm
Quote (RecenT @ Apr 9 2014 01:41pm)
mmm.. I'm trying to learn how you guys are doing it. I haven't learned some of these stuff.
I use using namespace std;, but I can see that you can use std:: instead.
I understand the setters/getters and had it set up like that.
But I don't understand why you use "int main(int argc, const char * argc). In fact, I'm not sure what that does for main and how that would be called.
Not really understanding the typedef short Ushort and typedef const Ushort cUshort.
:////////////


-When you include parts of the standard library, all the functions, constants, etc. are placed into a scope of names called a namespace. In the case of the standard library, the namespace is called "std". When you type "using namespace std;", that tells the compiler that the code you are going to write following it can directly "see" all those functions, constants, etc. that you included from the standard libraries. On the other hand, if you don't do "using namespace std;", you can still refer to the "std" namespace by prefixing your functions, constants, etc with "std::".

-"int main(int argc, const char* argv[])" is the standard function prototype for main. You can, however, omit argc and argv (as long as you don't use them) and nothing will happen. The purpose of those two variables is to receive the command line arguments for when you execute your program. "argc" stands for "argument count", while "argv" stands for "argument vector". For example, suppose you have a program named "foobar.out", and you run it like "./foobar.out". You had one command line argument, "./foobar.out", so "argc" will be 1 (argc==1), and "argv" will be an array containing one string (strcmp(argv[0], "./foobar.out")==0).

-typedef is just a way to abbreviate type names or rename them to whatever you want. If you really wanted to, for example, you could do "typedef int number;" and declare all of your integers as "number myInteger;", which would be equivalent to declaring them as "int myInteger;".
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 9 2014 12:19pm
Quote (KrzaQ2 @ Apr 9 2014 09:20am)
A lot :D

Not sure about the typedefs since you have cstdint in the standard, but whatever you like. The same goes about setters/getters that don't do anything, but that can be attributed to your personal/project guideline and aren't "simply wrong/right".


True, habits of C is presume. I need to work on completely moving over to C++/11 and not mixing C with C++ code.

I added the getters / setters for him because of this instruction: Set up a class of a customer with id number, service time, and arrival time. Add a parameterized constructor, all getters and setters, and a way to print the object(overload the << operator).

This post was edited by SelfTaught on Apr 9 2014 12:21pm
Member
Posts: 18,513
Joined: Dec 17 2005
Gold: 53.50
Apr 9 2014 02:17pm
Quote (poodaH @ Apr 9 2014 06:14pm)
-When you include parts of the standard library, all the functions, constants, etc. are placed into a scope of names called a namespace. In the case of the standard library, the namespace is called "std". When you type "using namespace std;", that tells the compiler that the code you are going to write following it can directly "see" all those functions, constants, etc. that you included from the standard libraries. On the other hand, if you don't do "using namespace std;", you can still refer to the "std" namespace by prefixing your functions, constants, etc with "std::".

-"int main(int argc, const char* argv[])" is the standard function prototype for main. You can, however, omit argc and argv (as long as you don't use them) and nothing will happen. The purpose of those two variables is to receive the command line arguments for when you execute your program. "argc" stands for "argument count", while "argv" stands for "argument vector". For example, suppose you have a program named "foobar.out", and you run it like "./foobar.out". You had one command line argument, "./foobar.out", so "argc" will be 1 (argc==1), and "argv" will be an array containing one string (strcmp(argv[0], "./foobar.out")==0).

-typedef is just a way to abbreviate type names or rename them to whatever you want. If you really wanted to, for example, you could do "typedef int number;" and declare all of your integers as "number myInteger;", which would be equivalent to declaring them as "int myInteger;".


const char* argv[] -- wouldn't that be a pointer that points to whatever argv[#] is? So I would have to manually set foobar be at argv[0] then I can use it as an output. I have to overload the operator << so I'm not sure if that I need to do that. Though like you said, there are many ways to approach this. I'll keep trying.

the typedef made perfect sense. Thanks!

I'll keep trying to work on this. I like doing this, but I don't know if I'd use it for future reference. I know I will for my later classes when building and programming robots, not so much on the real world cause I heard I use PLC instead. Mechatronics.
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 9 2014 03:25pm
Quote (RecenT @ Apr 9 2014 04:17pm)
const char* argv[] -- wouldn't that be a pointer that points to whatever argv[#] is? So I would have to manually set foobar be at argv[0] then I can use it as an output. I have to overload the operator << so I'm not sure if that I need to do that. Though like you said, there are many ways to approach this. I'll keep trying.

the typedef made perfect sense. Thanks!

I'll keep trying to work on this. I like doing this, but I don't know if I'd use it for future reference. I know I will for my later classes when building and programming robots, not so much on the real world cause I heard I use PLC instead. Mechatronics.


Not quite. Remember that a string in C is just an array of characters, terminated by the null terminator '\0'. Also remember that, for practical purposes, arrays are just immutable pointers. So the [] in "const char* argv[]" tells us that argv is an array. The * tells us that argv holds pointers, and in this case we know that those pointers are just pointing to strings that correspond to the arguments passed in from the command line.
And no, you don't have to set argc or argv. When you run the program from the command line, the operating system takes care of setting up those variables with whatever was on the command line for you.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Apr 9 2014 03:46pm
Quote
So the [] in "const char* argv[]" tells us that argv is an array.

Actually, no, it's a hint for the programmer at best. The C++ standard (and the C standard, but I cbf to find the quote) states quite explicitly:
Quote (n3337 §8.3.5 [dcl.fct]/5)
After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.

Therefore, int main(int argc, const char* argv[]) and int main(int argc, const char** argv) are exactly the same thing. Moreover, such a signature of the main function may be not portable, as it's not required by the standard to work (n3337 §3.6.1 [basic.start.main]/2)
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 9 2014 05:29pm
Quote (KrzaQ2 @ Apr 9 2014 05:46pm)
Actually, no, it's a hint for the programmer at best. The C++ standard (and the C standard, but I cbf to find the quote) states quite explicitly:

Therefore, int main(int argc, const char* argv[]) and int main(int argc, const char** argv) are exactly the same thing. Moreover, such a signature of the main function may be not portable, as it's not required by the standard to work (n3337 §3.6.1 [basic.start.main]/2)


I am aware of this, but for the purpose of explaining to someone who isn't clear on pointers, it's cleaner explaining it the way I did.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll