Class for measuring distance as feet and inches My homework question states:


  Develop a class to measure distance as feet (should be int), inches (should
  be float).  Include member functions to set and get attributes.  Include
  constructors.  Develop functions to add two distances.
  
  The prototype of the sum function is:

Distance Sum(Distance d);



Please check out my coding style and other aspects that can make this program better.

#include &lt;iostream&gt;
using namespace std;
class Distance
{
        private:
                int feet;
                float inch;
        public:
        Distance();
        Distance(int a,float b);
        void setDistance();
        int getFeet();
        float getInch();
        void distanceSum(Distance d);
};
int main()
{
        Distance D1,D2;
        D1.setDistance();
        D2.setDistance();
        D1.distanceSum(D2);
        return 0;
}
/*Function Definitions*/
Distance::Distance()
{
        inch=feet=0;
}
Distance::Distance(int a,float b)
{
        feet=a;
        inch=b; 
}
void Distance::setDistance()
{
        cout&lt;&lt;"Enter distance in feet";
        cin&gt;&gt;feet;
        cout&lt;&lt;endl&lt;&lt;"Enter inches:";
        cin&gt;&gt;inch;
}
int Distance::getFeet()
{
        return feet;
}
float Distance::getInch()
{
        return inch;
}
void Distance::distanceSum(Distance d)
{
        cout&lt;&lt;"feet="&lt;&lt;d.feet+feet&lt;&lt;endl;
        cout&lt;&lt;"inches="&lt;&lt;d.inch+inch&lt;&lt;endl;
}
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 a or b. Have the compiler enforce that promise by using const:

Distance::Distance(const int a, const float b)


Similiarly:

void Distance::distanceSum(const Distance &amp;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.


class Distance should probably be in a separate header/implementation file rather than putting everything in one main file.


Ninja comments. 


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:

#ifndef DISTANCE_H
#define DISTANCE_H
class Distance
{
    private:
       // feet and inch is one unit. 
       // invariant: inch_ &lt; 12.
       int   feet_;
       float inch_;
    public:
       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&amp; Add(const Distance &amp;d);
};
#endif


In distance.cpp implementation:

#include "distance.h"
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
{
    return feet_;
}

float Distance::getInch() const
{
    return inch_;
}

Distance&amp; Distance::Add(const Distance &amp;d)
{
    setDistance(getFeet() + d.getFeet(), getInch() + d.getInch());

    return *this;
}


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 feet_ and inch_ directly.