Please forgive me if my understanding is not up to par: |
No worries, it's all about learning - I am stoked you are willing to learn :+)
WRT data members:
What data members did I not initialize? The variables need to be public so that the user can change their values. Perhaps create local variables in main() and send them to a member function to set member values? |
Initialisation is slightly different from assignment. The initialisation list, initialised 3 member variables, then the rest were by assignment. To quote Scott Meyers :
Scott Meyers wrote: |
---|
The rules of C++ stipulate that data members of an object are initialized before the body of the constructor is entered. |
That is before the opening brace of the constructor. This applies to non built in types (ie class objects) which have their constructors called in the initialisation even if you don't have anything in the initialiser list. So when you do assignment inside the body of the constructor, all of the previous effort was wasted. For built in types like int or double, they are not guaranteed to be initialised at all. So it's best to initialise everything with the initialiser list, and it's good practice to do it in the same order that members appear in the class declaration.
Having public member variables is a really bad idea: Would you display all your bank account details & balances on the internet AND let ANYONE change them whenever they wanted? Instead, it is better to make them all private, and provide an interface (public functions) to allow access to only those that need it. Don't be tempted to have get / set functions for each member though. If you do need to change values after the object has been created, then consider having one function to change them all at once. However changing 1 variable at a time to see the effects could be a valid use of set functions.
WRT variable names:
I understand your point, but they are what they are to coincide with the variable names used in my resource materials. |
That's fine. I am always mentioning this because in my mind it relates to better understanding especially for others. I did exactly the same thing in one of my projects - named the variables as close as possible as to what was in the documentation. But I wonder for example:
a = 0.5 * b * h;
versus
TriArea = 0.5 *Base * Height;
. To me the latter is pretty Idiot proof, sometimes you might amaze yourself how a runtime error can occur because of insufficient variable naming.
WRT vector use:
I haven't used vectors much and was toying with their usage last night. It would be nice to not have to worry about the size of the array. |
Yes, and more importantly the memory management - you don't have to do it with any of the STL containers!!
WRT condition statements:
How are these different, other than being able to determine the value of "a" just by looking at the termination value? |
The standard idiom for doing something n times is:
for (int a = 0;a < n; ++a) {}
If you use the
<=
it will do it n +1 times. This is a problem when dealing with arrays, or any container which returns a size value, or uses a subscript operator
[]
because you will go past the end, and get a seg fault.
WRT function.cpp
Output file as an arguement is a great idea! |
If I'm understanding correctly, you would replace function.h/.cpp with another object which holds and outputs the final results. Is that correct? |
With that section, the idea was to have the class for just the data object. I am saying put a collection of the data objects elsewhere - in main() if you like, or in a class of it's own. So, if you had points, create a point class, but imagine it is only 1 point. Then put the collection of the points elsewhere. If it's in a class of it's own, then that class would have facilities to retrieve individual items, or do things like sort all of them or apply some function to them all. These would probably make use of the STL algorithms and functions to do this.
...don't have using namespace std;
--> Why? If the std namespace is the only one needed, shouldn't time and effort be saved by using it? |
What happens is that you bring in the entire std namespace (The whole of the STL!!) into the global namespace, thereby polluting it. It will cause you naming conflicts one day. Did you know that there is
std::distance
,
std::left
,
std::right
, just to name a few. If you had a function or variable name the same as these -> big problems. Look them up see what they do. This whole thing creates a drama that we were trying avoid by having the concept of a namespace!
So what to do instead? You can either put
std::
before each
std
thing - sounds like a pain, but you get used to it. This has the advantage of no editing required when you send someone a function snippet. Or you can do this after the includes:
1 2 3 4 5 6 7
|
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
// etc
|
Sometimes a mixture is worthwhile -do
std::find
if that only appears a few times in a file.
It is also a good idea to keep you own stuff out of the global namespace. Put you class declaration, and the definition of it's functions into a namespace declaration. Use the scope resolution operator to refer to things in your namespace. For example
MyNS::MyClass::MyFunction
. You can also use the using statement to make an alias for things:
using MyNamespace = mns;
Jeepers - I just saw your post at the top of this page.
Perhaps I am reading this wrong: |
If you have set functions, ensure that the result is re-calculated somewhere. This might be a good reason to have the answer as a function instead of a variable. I keep thinking about geometry objects, where things are dependent on each other: A circular arc defined by 3 points - you can't just move one of the points without invalidating the other info. This why I mentioned having private member data.
Vavg = mean wind velocity from a sample of experimental wind velocities |
If you program was going to manage this data as well (I understand it might not need to), then that implies a WindVelocity class, A class to hold a collection of that data, with functions to calc the average or other stats as necessary.
Some other things I do:
Name my classes with a leading C as in CWindVelocity. I am not the only one: Qt uses Q, KDE uses K
I like CamelCase for names.
Name member variables with a leading
m_
as in
m_Radius
This helps avoid the need for
this->Radius
in operator overloading.
Name any pointer variable with a leading
p
as in
pMyVariable
. Probably shouldn't be using pointers these days, unless they are smart pointers.
Any way hope you are having fun. I am: I finally have a job after much lollygagging about, now have to pack the car & drive to Darwin. I am in Melbourne, so that is a casual ~4,000km drive. Hopefully I won't be too discombobulated at the other end!!