// Listing 14.4
// Add Function
#include <iostream>
class Counter
{
      public:
             Counter();
             Counter(int initialvalue);
             ~Counter() {}
             int GetItsVal()const {return itsVal; }
             void SetItsVal(int x) {itsVal = x; }
             Counter Add(const Counter &);
             
             private:
             int itsVal;
                     
             };
                     
             Counter::Counter(int initialValue):
             itsVal(initialValue)
             {}
                                          
             Counter::Counter():
             itsVal (0)
             {}
             
             Counter Counter::Add(const Counter & rhs)
             {
                     return Counter(itsVal+ rhs.GetItsVal());
                     }
                     
                     int main()
                     {
                         Counter varOne(2), varTwo(4), varThree;
                         varThree = varOne.Add(varTwo);
                         std::cout << "varOne: " << varOne.GetItsVal()<< std::endl;
                         std::cout << "varTwo: " << varTwo.GetItsVal() << std::endl;
                         std::cout << "varThree: " << varThree.GetItsVal()
                         << std::endl;
                         std::cout << "Press Enter or Return to continue.\n";
                         std::cin.get();
                         return 0;
                         }

I was wondering if anybody could help me understand the above code a little bit better? The analysis is from my book. I have asked questions on the parts I don't understand. Many thanks in advance!!

Analysis:
The Add() function is declared on line 12. It takes a constant Counter reference,
which is the number to add to the current object.(which is varOne?)It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 35.( which is varThree?) That is, varOne is the object, varTwo is the parameter to the Add() function, and the result is assigned to varThree.

Return Counter(itsVal+ rhs.GetItsVal()); Is itsVal+rhs.GetItsVal()); VarOne + VarTwo? and is Return Counter (Var three)?

Recommended Answers

All 10 Replies


The Add() function is declared on line 12. It takes a constant Counter reference,
which is the number to add to the current object.(which is varOne?)It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 35.( which is varThree?) That is, varOne is the object, varTwo is the parameter to the Add() function, and the result is assigned to varThree.

Return Counter(itsVal+ rhs.GetItsVal()); Is itsVal+rhs.GetItsVal()); VarOne + VarTwo? and is Return Counter (Var three)?

It appears you have the correct answer, but in the Analysis that is written, it reconfirms what your questions are.

It takes a constant Counter reference,
which is the number to add to the current object.(which is varOne?)

Yes, the object that is calling the function, which is varOne, needs a reference parameter of type Counter.

It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 35.( which is varThree?)

Yes. Once the manipulation from the function is done, it returns a new object that will be saved to the variable on the left side of the assignment operator, in this case to be varThree.

Return Counter(itsVal+ rhs.GetItsVal()); Is itsVal+rhs.GetItsVal()); VarOne + VarTwo? and is Return Counter (Var three)?

Yes, the value from the first object plus the value from the second object is saved and returned to varThree

You got this example from "Teach yourself C++ in 21 days". Do you really think that is possible? Really?

By seeing the quality of the code in this simple, short example, it doesn't baud well for the remainder of the book.

Anyways, I think Saith covered what was needed. You might want to take a look at this thread for better book suggestions.

Thanks for your help. And I got the example from "Teach yourself C++ in 24 hours"

I'm still finding this confuseing. In the Counter::Counter constructor on lines 19-20 is initialValue assigned to it'sVal? and it's used to initialize varOne and varTwo to (2) and (4) and the second constructor on lines 23-25 initializes varThree to (0)?

What I find confuseing is the Add fuction itself. It takes a const Counter reference which is varTwo but this part is most confuseing:

return Counter(itsVal+ rhs.GetItsVal());

is itsVal in the Add function VarOne and the initialized (2) from the Counter::Counter(int initialValue): itsVal(initialValue) {}constructor? and is it added to a const Counter reference of VarTwo? And is the varTwo that is passed to the add function actually varTwo or a const Counter reference of the value in VarTwo? It's the

itsVal+

part in

return Counter(itsVal + rhs.GetItsVal());

that I'm finding most confuseing. I like to understand something completely and I feel like I'm missing something with this.

I think itsVal in

return Counter(itsVal+ rhs.GetItsVal());

is the same itsVal in the initialized varOne(2) in the

Counter::Counter(int initialValue):itsVal(initialValue)

? I'm not sure though.

Thanks a lot for the replies.

is itsVal in the Add function VarOne and the initialized (2) from the Counter::Counter(int initialValue): itsVal(initialValue) {}constructor?

Yes.

and is it added to a const Counter reference of VarTwo?

No, VarOne::itsVal is added to VarTwo::itsVal, and that result is used to initialize a Counter variable for return from the Add member function (using the Counter constructor). VarTwo::itsVal is retrieved through the member function GetItsVal().

I'm getting there gradually lol...
the first object is created varOne(2) so
void SetItsVal(int x) {itsVal = x;} varOne itsVal = 2
then the second object is created varTwo(4) so
void SerItsVal(int x) {itsVal = x;} varTwo itsVal = 4
then the last object is created varThree which is initialized to 0?

Counter Counter::Add(const Counter & rhs)
{ return Counter (itsVal+ rhs.GetItsVal());
}

Var One itsVal(2) is added to Var Two itsVal(4) which is retrieved by the member function GetItsVal () and that result is used to initialize a Counter variable for return from the add member function using the Counter constructor (which one?).. I'm guessing Counter::Counter(int initialValue): itsVal(initialValue)?

Also why does it say(in my book) the Add function takes a constant Counter reference? when an object or Counter variable VarTwo is passed to it?

Many thanks again!! And sorry I'm a slow learner..lol.

then the last object is created varThree which is initialized to 0?

Nope, varThree is copy constructed from the result of Add(). If you unroll everything manually (including removing the temporary), this:

Counter a(2), b(4);
Counter c = a.Add(b);

Is functionally equivalent to this:

Counter a(2), b(4);
Counter c(a.GetItsVal() + b.GetItsVal()); // ie. Counter c(2 + 4);

I'm guessing Counter::Counter(int initialValue): itsVal(initialValue)?

Good guess.

Also why does it say(in my book) the Add function takes a constant Counter reference? when an object or Counter variable VarTwo is passed to it?

You pass a Counter variable, but that variable is then bound to a constant reference to avoid making an unnecessary copy of the object. Just like this:

Counter a(2), b(4);

// Bind b to a const reference manually
const Counter& temp = b;

Counter c = a.Add(temp);

Thanks. I think I get it now. What confused me is the itsVal in:

return Counter(itsVal+ rhs.GetItsVal());

isn't very explicit in it's belonging to VarOne. Var Two is obviously passed to the add function and is bound to a counter reference to avoid making an unnecessary copy of it and rhs.GetItsVal(); is therefore basically equivalent to VarTwo.GetItsVal() and retrieves VarTwo itsVal which is added to VarOne itsVal and VarThree is assigned the result of the Add function. Do I sound like I get it now? lol. Thanks a lot for your help..I really appreciate it!!

commented: Good job, it get's more exciting from here on out :D +5

Do I sound like I get it now? lol.

I think you've got the concept, yes. You're kind of over-complicating things, but it takes a certain measure of understanding to simplify. ;)

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.