hello!
I like to define the Distance class and overload binary + operator to add the two distances using friend function.
my program is here. But it does not work.
The error message said that "undefined symbol feet". What is the problem?

#include<iostream.h>
class Distance
{
 int feet;
 float inches;

 public:
 Distance()
 {
  feet=0;
  inches=0.0;
 }

 Distance(int ft,float in)
 {
  feet=ft;
  inches=in;

  }
  void showdata()
  {
   cout<<"\nFeet:"<<feet;
   cout<<"\nInches:"<<inches;
  }
  friend Distance operator+(Distance );
 };


Distance operator+(Distance d)
{
 Distance temp;
 temp.feet=feet+d.feet;
 temp.inches=inches+d.inches;
 while(temp.inches>=12)
 {
  temp.inches-=12;
  temp.feet++;
 }
 return temp;
}


int main()
{Distance d3;
 Distance d1(1,0);
 Distance d2(1,1);
 d3=d1+d2;
 d3.showdata();
 return 0;
}

Recommended Answers

All 7 Replies

Which line is it having a problem with?

Member Avatar for iamthwee

Why are you using the friend for overloading +

Your operator+ method is not actually a member of Distance.
You need to define it as Distance Distance::operator+(Distance d)

And you should use iostream, not iostream.h.

#include <iostream>
using namespace std;

Thanks for replys. But I haven’t got it.
I wrote this program because the assignment question I have to do is like that
“Define the Distance class and overload binary + operator to add the two distances using friend function.”

Hello my friend!

Below is your code corrected and commented. You were thinking of operator+ as a member function, this is why you tried to access inches and feet because you supposed they belong to an object that will call this operator when used as a left operand of +.

Your mistake was considering the friend function as a member and it is not. Binary operators (those who take two operands) are declared using one parameter when they are MEMBER functions (like what you did), but when they are not members (i.e friend functions) they are declared with 2 paramerters, namely the left and right operands.

#include<iostream>    //I don't know what compiler you are using
using namespace std; //but this is how this works for me
class Distance
{
 int feet;
 float inches;

 public:
 Distance()
 {
  feet=0;
  inches=0.0;
 }

 Distance(int ft,float in)
 {
  feet=ft;
  inches=in;

  }
  void showdata()
  {
   cout<<"\nFeet:"<<feet;
   cout<<"\nInches:"<<inches;
  }
  friend Distance operator+(Distance, Distance ); //operator+ is a friend function to this class but NOT a member function.
 };


//friend means that it can access the private members belonging to a class in
//which the function is declared as a friend in it.

//friend functions are not associated with any object instantiated form a class,
//so you can't use inches, feet directly, but you have to take two Distance objects
//as parameters.
//this is the case with any binary operator declared as friend.
Distance operator+(Distance d1, Distance d2)
{
 Distance temp;
//notice that we are accessing private members of Distance objects d1 and d2,
//because operator+ is a friend to the Distance class, from which d1 and d2
//are instantiated.

 temp.feet= d1.feet + d2.feet; 							
 temp.inches= d1.inches + d2.inches;
 
 while(temp.inches>=12)
 {
  temp.inches-=12;
  temp.feet++;
 }

 return temp;
}



int main()
{Distance d3;
 Distance d1(1,0);
 Distance d2(1,1);
 d3=d1+d2;
 d3.showdata();
 return 0;
}

So the main change I made is to add another parameter for the + operator. Now the operator has 2 parameters: the first is the left operand of the + operator and the second is the right operand of the + operator.

Note that friend functions are NOT members of a class, so they are not associated with any Object, and this is why you cannot use inches and feet directly. Only when you declare operator+ as a member (taking only 1 parameter explicitly -the right operand- and the left operand being the 'calling' object associated with the function, only is this situation you are allowed to use the 'calling' object members.

Hope this helps, please indicate the problem as resolved if this helps you resolve it.

Mazin

commented: Well organized and explained lucidly +1

Hi Mazin,

Thank u very much.
I have got iit.
Now I understand very well about friend function and I will not do such mistakes again.
Thanks again.
Moe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.