Hi,
That looks OK - Good Work!!, but there are some things I would do differently:
If the Location struct was out side the class, it could be reused for other things.
It's a good idea to separate the class definition and the member function implementations, see this post:
http://www.cplusplus.com/forum/general/248394/#msg1094846
if (speed == 0.0)
That might not work for all doubles, they aren't represented exactly. What you need is a "Equal within a precision " function. If the absolute value of Value - expected value is less than precision, then it returns
true
In general stick to relational comparisons, not equality.
Is that ok, or is camel case expected in C++? |
What you have done is fine, there are no expectations, people / organizations have personal preferences / coding standards and they all vary. The isocpp superfaq mentions things not to do. I like PascalCase, others like all lower with underscores - that's what the C++ STL does. I like PascalCase, so it
doesn't look like STL.
Also, I noticed in your initilization list you used {} instead of () like Ganado. What is the difference? |
Parentheses means direct initialization, braces are used for several different initializations. There are lots of ways to initialize:
There is an initialization section.
https://en.cppreference.com/w/cpp/language
That site might be hard to read & comprehend, because it is a technical reference based on the C++ standards. Stick with it though.
Look at the reference on this site too, the examples might be easier.
Also, it sounds like objects with multiple inheritance would need giant initialization lists. Is this the way it is done, or is there another way around it? |
Note that multiple inheritance is different from several levels of direct inheritance. Multiple means inheritance from 2 or more bases classes at the same time. As in C inherits from A and B (multiple) versus C inherits from B which inherits from A.
Generally we try to avoid big inheritance trees, and be careful with multiple inheritance. There are other ways of doing things, like Design Patterns, but that is more advanced.
It's good you don't have
using namespace std;
but it also good to not have
using std::cout;
either, only because it's easy to have 20 of them at the start of the file (by using different containers and algorithms), that gets tiresome. Just put
std::
before every std thing. That's what all the experienced coders do, you might as well start now :+)
Ideally, your code should be in it's own namespace/s. You might get brownie points for doing that :+)
With variable and function names, choosing meaningful names can lead to the code telling a story of what it does. This helps greatly for humans to understand it, especially if it's a maintenance coder (not you) years later.
With formatting:
If there are several arguments, I like to put them 1 per line, it's easier to read:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public:
Vehicle(double length_arg,
double width_arg,
double x_arg,
double y_arg,
double speed_arg,
double angle_arg)
:
vehicle_body(length_arg, width_arg),
position(x_arg, y_arg),
speed(speed_arg),
angle(angle_arg)
{}
|
It looks nice if they are all lined up, tricky here because the font is not fixed width.
I do the same thing with std::cout when there are multiple things to print:
1 2 3 4 5 6 7 8 9 10
|
void report()
{
std::cout << "Vehicle length: " << vehicle_body.length << " m\n"
<< "Vehicle width: " << vehicle_body.width << " m\n"
<< "Vehicle X Position: " << position.x << "\n"
<< "Vehicle Y Position: " << position.y << "\n"
report_speed();
std::cout << "Vehicle angle: " << angle << "\370\n"
<< "--------------------------" << "\n";
}
|
Notice I didn't use std::endl (it's slow), just a \n instead.
Basically good use of vertical and horizontal white space and anything else to make the code pleasing to look at will be appreciated by all kinds of people reading the code, your boss for instance :+)
Have fun !!