Convert inches to feet?

Hi i am wondering how to convert inches to feet from the user input? i am doing overload programs with main.cpp, distance.h and distance.cpp.
here is my code

distance.cpp

#include "Distance.h"
#include <iostream>

Distance::Distance() {
setFeet(1);
setInches(1);
}

Distance::Distance(int feet, int inches) {
setFeet(feet);
setInches(inches);
}

void Distance::setFeet(int feet) { this->feet = feet; }
void Distance::setInches(int inches) { this->inches = inches; }
int Distance::getInches() { return inches; }
int Distance::getFeet() { return feet; }

int Distance::getTotal() { return getFeet() * getInches(); }

Distance Distance::operator + (Distance Distanceb) {
Distance temp;
temp.setFeet(getFeet() + Distanceb.getFeet());
temp.setInches(getInches() + Distanceb.getInches());
return (temp);
}

//output
ostream &operator << (ostream &out, Distance Distance)
{
out << "Distance: " << Distance.getFeet() << " Feet" << " + " << Distance.getInches() << " Inches";
cout << endl;

int temp;

return out;

}

//input
istream &operator >> (istream &in, Distance &c)
{
int temp;
cout << "Enter Feet: ";
in >> temp;
c.setFeet(temp);

cout<<"Enter Inches: ";
in >> temp;
c.setInches(temp);

return in;
}


Last edited on
According to your question, inches to feet is simple enough, simply divide by 12. Feet to inches, multiply by 12. So 1 foot equals 12 inches (1 * 12 = 12) and 36 inches equals 3 feet ( 36 / 12 = 3)

As for your code, I'm not seeing a destructor nor am I seeing a copy constructor. These two things are just as important as the constructor. You also need to overload the assignment operator in case you instantiate your class like so:Distance *foo = new Distance();
Also, why are you redefining the ostream shifting operators? (<< and >>) They don't appear to be part of your class and so re-defining them will most likely give you and error at compile time and is a bad idea. Can we also please see the rest of the source code (.h/.hpp and .c/.cpp)? And please use the code tags (the <> button)

Thanks and best of luck with your program.
Overloading the operator to work with user-defined data types (classes) is pretty standard when making classes, isn't it?
It is, but the operator being overloaded doesn't appear to be part of his class. But to that effect, we don't have the header to examine and confirm/deny this.
If it's not an actual member function then the only thing that it would need is to be made a friend function so it can access the private data members. Other then that, overloaded operators don't NEED to be member functions. In fact, you wouldn't even need friend if the overloaded operator calls the getters to retrieve the feet and inches.

Only other things I'd change would be to change the parameters on the function to be different names then the data members. Using the same name for different variables can obfuscate code and make it harder to read.

Oh, and your gettotal() function. If you are storing inches and feet, returning feet*inches won't represent an accurate total for the how many inches the class holds. I'd change that to return (feet * 12 + inches)
Topic archived. No new replies allowed.