Connecting Tech Pros Worldwide Help | Site Map

Simple question about classes...

Newbie
 
Join Date: Oct 2009
Posts: 13
#1: Oct 17 '09
Hello,
I'm studying C++ all by myself. I'm studying from C++ primer (4e) by S.B.Lippman, J. Lajoie and B.E.Moo.

In chapter-1, there's a question whose answer I want to confirm. The question says:
Quote:

Originally Posted by C++ primer (4e) Exercise section 1.6, Que 1.26

In the bookstore program we used the addition operator and not the compound assignment operator to add trans to total. Why didn't we use the compound assignment operator?

I think the answer to this question is that compound assignment operator is not a valid operation that is allowed on Sales_item objects. But I'm sure whether this is correct.

Am I getting it right? If not, where am I wrong?

Thank you very much.
SchoolOfLife
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#2: Oct 18 '09

re: Simple question about classes...


You maybe right , then again you may be wrong.
Post the code snippet so we can understand what you are talking about... and use code tags.
Newbie
 
Join Date: Oct 2009
Posts: 13
#3: Oct 18 '09

re: Simple question about classes...


Hello,

Thanks...

I don't know much about C++ classes yet as I'm presently studying chapter-2 from the book. And so, I'm confused with this question.
The book says that the class supports addition (+), input (>>), output (<<), assignment (=) operators and a same_isbn member function. But I just want to confirm that I'm right.

Anyways, I'm attaching the file "Sales_item.h" (renamed to 'Sales_item.h.txt' because forum software was refusing to attach file with .h extension) that is available on book's website. If you can, please have a look.

Thanks again..
SchoolOfLife
Attached Files
File Type: txt Sales_item.h.txt (4.1 KB, 15 views)
Newbie
 
Join Date: Oct 2009
Posts: 13
#4: 4 Weeks Ago

re: Simple question about classes...


________________b u m p_________________
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,158
#5: 4 Weeks Ago

re: Simple question about classes...


Well the class Sales_item implements the compound operator += (line 52 of attached file) so the answer can't be that it is an invalid operation.

I guess we would need to see the calling code although the main reason for using + rather than += is you need to keep the original value of the lhs of the equation.
Newbie
 
Join Date: Oct 2009
Posts: 13
#6: 4 Weeks Ago

re: Simple question about classes...


Thanks....

Quote:

Originally Posted by Banfa View Post

Well the class Sales_item implements the compound operator += (line 52 of attached file) so the answer can't be that it is an invalid operation.

oh yes...I now understand atleast some part of Sales_item.h file.

Quote:

Originally Posted by Banfa

I guess we would need to see the calling code although the main reason for using + rather than += is you need to keep the original value of the lhs of the equation.

I read some C long time ago, but I dont get what you mean by "using + rather than += is you need to keep the original value of the lhs of the equation". Can you please explain this to me?

Anyways, here's the code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Sales_item.h" 
  3.  
  4. int main() 
  5.         //  declare variables to hold running sum and data for the next record 
  6.         Sales_item total, trans; 
  7.         //  is there data to process? 
  8.         if (std::cin >> total) { 
  9.             // if so, read the transaction records 
  10.             while (std::cin >> trans) 
  11.                 if  (total.same_isbn(trans)) 
  12.                    //  match: update the running total 
  13.                    total = total + trans; 
  14.                 else { 
  15.                    //  no match: print & assign to total 
  16.                    std::cout << total << std::endl; 
  17.                    total = trans; 
  18.                 } 
  19.             //  remember to print last record 
  20.             std::cout << total << std::endl; 
  21.          } else { 
  22.             //  no input!, warn the user 
  23.             std::cout << "No data?!" << std::endl; 
  24.             return -1;  //  indicate failure 
  25.          } 
  26.          return 0;
  27. }
Line 14 has got the statement that is our present matter of discussion.

Thanks again for you input.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,158
#7: 4 Weeks Ago

re: Simple question about classes...


Quote:

Originally Posted by SchoolOfLife View Post

I read some C long time ago, but I dont get what you mean by "using + rather than += is you need to keep the original value of the lhs of the equation". Can you please explain this to me?

For the addition operator you have the form

answer = lhs + rhs;

After the operator has been evaluated both the lhs and the rhs have the same value as the did before the operation. However if you use the compound addition operator += then you have

lhs += rhs;

Which is equivalent to

lhs = lhs + rhs;

After the operator has been evaluated you have lost the original value of lhs because it has been overwritten by the result of the addition.


As to why the code uses + rather than += I see no reason. In fact it looks to me as if it would be more efficient to use += and achieve exactly the same result.

More efficient because the

total = total + trans;

calls

operator+
operator+= (internally to operator+)
operator=

where as

total += trans;

only calls

operator+=
Newbie
 
Join Date: Oct 2009
Posts: 13
#8: 4 Weeks Ago

re: Simple question about classes...


Thanks Banfa.
Although the original question is still unanswered, but I'm glad that I learnt a few concepts from this discussion (like += is more efficient than +, the concept; in += original value of lhs is overwritten).
Currently, I'm studying chapter-3 from C++ primer, so I hope that once I'm done with this book, I will get some answer. Till then, I suppose we can 'pause' this discussion. If I find answer, I will post it here.

Thanks again...
Reply