Reputation: 620
I was writing a program for adding 2 objects of a class.
//program for adding 2 objects data of same class
#include<iostream>
using namespace std;
class distances
{
private:
int feet;
float inches;
public:
distances() //constructor
{
feet = 0;
inches = 0.0;
}
distances(int f, float i) //constructor
{
feet = f;
inches = i;
}
void get_data() //taking value
{
cout << "Enter the distance in feet and inches :: " << "\n";
cin >> feet >> inches;
}
void show_data() //showing data
{
cout << "The distance in feet is ::" << feet
<< " and in inches is :: " << inches;
}
void add(distances d1, distances d2); //adding to objects
};
void distances::add(distances d1, distances d2)
{
inches = d1.inches + d2.inches;
feet = 0;
while(inches >= 12)
{
inches = inches - 12;
++feet;
}
feet += d1.feet + d2.feet;
}
void main()
{
distances d1, d2, d3;
d1.get_data();
d2.get_data();
d3.add(d1, d2);
d3.show_data();
getch();
}
My program worked fine but my sir told that my approach of adding 2 objects was wrong ,although he didn't tell why.He told me that my approach won't work when I will add more objects. I don't know why my approach was wrong.My friend told me that my problem might be in the line d3.add(d1,d2);
Is that true?
My second problem was that when I used class name,function name and constructor name as distance instead of distances then following error was coming
1>c:\users\abc\documents\visual studio 2010\projects\pass\pass\pass.cpp(47): error C2872: 'distance' : ambiguous symbol
1> could be 'c:\users\abc\documents\visual studio 2010\projects\pass\pass\pass.cpp(6) : distance'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(899) : iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)'
Upvotes: 2
Views: 5733
Reputation: 9380
You may overload the +
operator as:
distances& operator+(const distances& obj1)
{
inches+=(obj1.inches)%12;
feet+=obj1.feet + (obj1.inches)/12;
return *this;
}
The specific use of returning a reference to this
pointer may help you to achieve
distances a = b + c + d;
Upvotes: 1
Reputation: 8287
You are getting the error as there is already a class defined with the name 'distance' in visual studio
's xutility
(don't know what this is, just got this from the error message).
Also you are re-inventing the division :), in instead of a while loop you can just do
carry = int(inches / 12);
inches = int(inches) % 12;
feet += carry;
Also as you have been 'told' that this is wrong approach I can see 2 suggestions here. One, As this only works for 2 objects, you can make it work for multiple objects' addition, by adding in a loop. Instead of passing 2 objects as parameters, pass an array.
Two, as other answers pointed out, overloading the +
operator is more intuitive.
Upvotes: 3
Reputation: 382
0xc0de answered the reason for the Visual Studio error and provided a good tip to simplify the calculation of feet and inches.
What others have pointed out is that the way you did it limits you to always adding exactly two distances into the existing distance. It's cleaner and you can chain additions limitlessly by providing operator+, operator+= and operator= on your distances class. This is a very common method for extending classes. It is true that you are only adding the internal values of the object, but that is very often the case.
Upvotes: 1
Reputation: 322
I think what your "sir" was trying to tell you is that with your approach something like following wont be possible
Distance d1,d2,d3,d4;
d4 = d1 + d2 + d3;
I would prefer to overload "+" operator rather than defining an Add function. Doing
d4 = d1 + d2 + d3
is so much intuitive.
Regarding your second question, when you rename distances to distance it conflicts with STL distance function. This function is unside std namepspace but since you have wirtten
using namespace std
it has become visible and hence the conflicy
Upvotes: 2