.... I don't understand the syntax yet, for instance, "std::string&" (or any such code that uses ::) is completely foreign to me, we haven't been taught to use that yet. |
That is all to do with
namespaces. The idea of them is to help naming conflicts. In a larger project, we don't want 2 coders to use the same variable
unit
, so they each put their code in it's own namespace:
fatmurphy::unit
and
TheIdeasMan::unit
. The problem with
using namespace std;
is that there exists for example
std::distance
- we have a problem, Houston :+). There are dozens of example like this, so put
std::distance
if you want the distance between two item indices in a container, and without std:: to refer to something else. Personally, I use PascalCase for my variable and function names, so they will never conflict with the STL.
As far as learning from people like you things which she hasn't taught yet, she is all for it, actually she gives extra credit for adding bits that are more advanced than what we have learned in class, ... |
Well, you are
very lucky there IMO, so many teachers don't do that. Usually they want you to write your own sort function, not use the one from the library, in which case you won't learn anything. So that is why I suggest you won't get away with using
std::find
. You could try to write own version that does the same thing. I guess the main thing is that you need to be able to explain
why you used a particular syntax or feature, for example why you now put
std::
before every std thing, and your variables/ functions/ classes are all in their own
namespace
, and why you use
const
or
constexpr
. If I was your teacher, I would be over the moon if you had these concepts down already.
The reference (ampersand in
const std::string& DimArg
) is
conceptually like passing the address (a pointer) of the variable, instead of passing the whole thing by value. A std::string could potentially have thousands or even millions of characters, so we pass it's address in memory (64 bit) instead of copying KB or MB of data. We use this passing by reference for any of the STL containers or classes, or any class that you have created.
The use of
const
(called
const
correctness) is a very useful thing, it actually helps one code properly. If one tries to change something which is const the compiler will produce an error. It's not just for function arguments, one can use it for ordinary variables, and class functions (it doesn't change any class member values). There is also constexpr :
1 2
|
constexpr double PI = acos(-1.0);
constexpr double PIOverTwo = PI / 2.0;
|
constexpr
is stronger than
const
, it is set in stone at compile time, where as
const
is able to be cast away with
const_cast
, the latter is inadvisable though.
You could
constexpr
for you conversion values between the units.
The other concept to think about: At this stage you will be writing programs that use a very small number of variables / data. In the real world, programs deal with
millions or even
billions of pieces of data. Just keep that in the back of your mind for now :+)
Good Luck !!