Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding objects data of a class?
    primarykey
    data
    text
    <p>I was writing a program for adding 2 objects of a class.</p> <pre><code>//program for adding 2 objects data of same class #include&lt;iostream&gt; 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 &lt;&lt; "Enter the distance in feet and inches :: " &lt;&lt; "\n"; cin &gt;&gt; feet &gt;&gt; inches; } void show_data() //showing data { cout &lt;&lt; "The distance in feet is ::" &lt;&lt; feet &lt;&lt; " and in inches is :: " &lt;&lt; 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 &gt;= 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(); } </code></pre> <p>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 <strong>d3.add(d1,d2);</strong></p> <p>Is that true?</p> <p>My second problem was that when I used class name,function name and constructor name as <strong>distance instead of distances</strong> then following error was coming</p> <pre><code>1&gt;c:\users\abc\documents\visual studio 2010\projects\pass\pass\pass.cpp(47): error C2872: 'distance' : ambiguous symbol 1&gt; could be 'c:\users\abc\documents\visual studio 2010\projects\pass\pass\pass.cpp(6) : distance' 1&gt; or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(899) : iterator_traits&lt;_Iter&gt;::difference_type std::distance(_InIt,_InIt)' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload