Convert float to distance

Hi all! here is my code(converting float to distance). i try to use operator to solving that. But it cannot work. So please tell me where is wrong!
#include<iostream.h>
#include<conio.h>

class Distance
{
private:
int feet;
float inches;

public:
Distance(){};
~Distance(){};
Distace(int ft, float inch)
{
feet=ft;
inches=inch;
}
void getdist()
{
cout<<"\nFut="; cin>>feet;
cout<<"\nInch="; cin>>inches;
}
Distance operator=(float);
};

Distance Distance::operator=(float p1)
{
Distance t;
float k;
k=p1/2.54;
t.feet=k/12;
t.inches=k%12;
return(t);
}
void main()
{
Distance dist1;
float s;
cin>>s;
dist1=s;


}




























You're converting cm to imperial right?

It's more conventional to do the conversion in a constructor, using an operator is ok I guess. The real problem is your conversion algorithm is incorrect.
The assignment operator doesn't make sense. I'm not sure where to begin. First, conceptually an assignment operator should be used for assigning objects of the same type. Secondly, in your assignment operator you create and return a temporary object without modifying the actual object that you are trying to assign to which does nothing useful. operator= is invoked for dist1 but you don't even attempt to modify dest1. You need to use
this->feet and this->inches to modify the object.
Start with this tutorial on classes and read both parts. You'll have to search the web for other articles and threads on assignment operators because you are misunderstanding the concept.
http://cplusplus.com/doc/tutorial/classes/
Thank you Kempofighter!
Topic archived. No new replies allowed.