When designing a class for your program, whether it's for homework or a real production application, you want to always consider how that class is going to be use and what its responsibilities should be. Each function and method should do one thing/task and it should be reflected by the method's name. Additionally, you want to keep to a consistent easy-to-read coding style when you actually start writing the code. With those two points in mind here's some things to consider in your code:
Distance::distanceSum is doing two tasks here.
It's not only summing feet and inches but it's printing them out as well.
- Unnecessary type conversion.
In your default Distance() ctor, there's an implicit conversion because you're assigning
0 to a float type. You should have gotten a warning from your compiler. Consider using an initializer list like so:
Distance::Distance() : feet(0), inch(0.0)
- Code doesn't take advantage of const correctness.
Which parameters aren't suppose to change? Which methods will be modifying your Distance class? For example, your
Distance::Distance(int a,float b) isn't changing
b. Have the compiler enforce that promise by using const:
Distance::Distance(const int a, const float b)
void Distance::distanceSum(const Distance &d);
- Inconsistent indentation and spacing.
Consider indenting the methods under public: the same way you did with private:. Add spaces to your assignments to help readability. eg.
feet = a;
- No module separation by file.
Distance should probably be in a separate header/implementation file rather than putting everything in one main file.
Comments? What comments? Exactly. Consider adding a block comment at the top of your Distance class that explains the purpose for its existence. The block comment should answer questions like how is this class suppose to be used and what details is it abstracting away? Adding a comment to clarify how the feet and inch data members are going to be used. For example, it's not clear if your distance class is maintaining the same distance measurement but with different units or it's really meant to be used as one whole unit. eg. 6 feet 2 inches or 6 feet 72 inches?
With the above considerations, here's one way I would refactor your code:
In distance.h header file:
// feet and inch is one unit.
// invariant: inch_ < 12.
Distance(const int feet = 0, const float inches = 0.0);
void setDistance(const int feet, const float inches = 0.0);
int getFeet() const;
float getInch() const;
// returns this instance. Permits method chaining for Distance class.
Distance& Add(const Distance &d);
In distance.cpp implementation:
Distance::Distance(const int feet, const float inches)
: feet_(feet + inches / 12), inch_(inches % 12)
void Distance::setDistance(const int feet, const float inches)
feet_ = feet + inches / 12;
inch_ = inches % 12;
int Distance::getFeet() const
float Distance::getInch() const
Distance& Distance::Add(const Distance &d)
setDistance(getFeet() + d.getFeet(), getInch() + d.getInch());
Here are the major changes above:
- Distance no longer uses cin/cout for explicit IO. You can push that code into main.
- class definition and implementation are now in their respectively named files.
- Removed an extra constructor definition by taking advantage of default parameters.
- feet and inch data members are used together to represent the measurement in distance. inch cannot be > 12 because that would mean there's enough for a foot. We enforce this by dividing feet and inches by 12 when setting distance's data.
- const is used to clearly indicate what can and can't change the distance object.
- Changed distanceSum to Add to better reflect what it's doing. Notice that Add is implemented only through Distance's public methods -- it does not manipulate