2

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)'
6502
  • 112,025
  • 15
  • 165
  • 265
Freedom911
  • 610
  • 2
  • 12
  • 26
  • 1
    `main` is required to return an `int`. The proper way to add objects is to overload `operator+` and `operator+=`. – chris Aug 15 '13 at 06:15
  • sorry chris ,i wanted to ask how to add value of 2 objects not the object itself. – Freedom911 Aug 15 '13 at 06:18
  • With a name like `add`, I would much prefer an operator approach to a Java approach. What your critic is getting at is probably that you don't support chaining. – chris Aug 15 '13 at 06:20
  • 1
    For your second question: you should consider removing the `using namespace std;` as it brings in a lot of stuff. See [this question](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) for example. You get used to the prefix `std::` and IMO it is easier to read afterwards, and you don't get such naming conflicts. – mr_georg Aug 15 '13 at 06:47

4 Answers4

3

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.

0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • 1
    The problem was not the division, but the modulo operator. Did you try your code using say `3.5` inches? – 6502 Aug 15 '13 at 06:36
  • Yeah, my bad. Too many years after last c/c++ coding, and too much into python :( – 0xc0de Aug 15 '13 at 06:39
  • You keep making the wrong correction. A correct solution could be `inches -= carry * 12;` instead – 6502 Aug 15 '13 at 06:40
2

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

mshriv
  • 332
  • 1
  • 2
1

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.

Michael Mathews
  • 382
  • 2
  • 7
1

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;
Saksham
  • 9,037
  • 7
  • 45
  • 73