Can someone tell me what I'm not getting in this program
All, being brief as possible, I’m working on an exercise from C++ Primer Plus Sixth Ed. by Stephen Prata. Chapt 10 Ex 6 for those playing along at home. I wasn’t sure what the exercise was asking. I found someone online who had done the exercises. I thought seeing the program in action, I’d understand the requirement and recreate my own. The program I downloaded works perfectly. My lack of understanding of why is works is my issue. I’m hoping someone here can clear up what I’m not understanding.
MOVE.H class Move{ private: double x; double y; public: Move(double a = 0, double b = 0); // sets x, y to a, b void showmove() const; Move add(const Move & m) const; void reset(double a = 0, double b = 0); // resets x,y to a, b }; MOVE.CPP (there are other class methods that deal with display and reset. I excluded them to conserve space.) Move::Move(double a, double b) { x = a; y = b;} Move Move::add(const Move & m) const { double m_x, m_y; m_x = this-\>x + m.x; m_y = this-\>y + m.y; Move newMove(m_x, m_y); return newMove; } MAIN.CPP (There of other line items for display etc.) Move move1; //Nothing sent so defaults to (x=0,y=0)per the constructor Move move2(1.5, 2.5); // constructor set move2 x =1.5 & y=2.5 Move move3(1.5, 2.1); // constructor set move3 x=1.5 & y=2.1 Move move4(move2.add(move3)); // this is where my confusion lies. When creating move4, I expected the constructor to look for nothing being sent or a combination of 2 doubles. Move2::add returns a reference or address of a newly created Move class called newMove. How does the constructor of move4 know to extract the x and y from newMove of class move2. The program works as expected. Move4 X=3 and Y=4.6. I’m obviously missing something. One of the downsides to learning without a teacher / tutor. Thanks. |
you ought to use code tags.
The constructor expects two doubles, the default values are 0. It will assign the two values to the internal/private variables x and y. This statement is valid for move1, move2 and move3. move4 is a different case, because no variables were specified. This implies a different constructor, which will use a class Move as input (instead of two doubles). This kind of constructor is called copy constructor and will copy/initialize the variables from the input class. See for example here: https://www.geeksforgeeks.org/copy-constructor-in-cpp/ (Also I think you can remove backslashes, Code:
Move Move::add(const Move & m) const |
|
And, to strongly(!) add to what @pan64 is saying, I definitely think that you should create a new Constructor case which accepts a Move object as its argument.
This is obviously something "that you would like to do, in the name of expressiveness," and so the logic to do this ought to appear one time – in an appropriate custom Constructor. "So be it." Then if, over time, the handling of this particular use-case should ever change, you would only need to modify the source-code once, in that Constructor. Always: "Say what you mean ..." And let the compiler then sort it all out. Your coding should most-naturally express "human understanding and expressiveness," burying the details in the implementation thereof. |
Thanks All,
Thanks pan64. This makes sense and is probably something in the chapter that I overlooked. I knew constructors were automatically created if there wasn't one coded. I was unaware that it would create one specific to a need at the time of an instance creation. Thanks again. |
The code is illustrating a pitfall of C++: overloading may be easy for the compiler, but it is hard for humans to understand.
In practice, one calls functions with the same number and types of arguments. Consistency is important for the reader. Ed |
Quote:
Anyway, the answer is that newMove returns a Move object, and that gets passed into Move's compiler-provided default copy constructor when you create move4. As pan4 said. |
EdGr. I agree with you.
I'm a novice when it comes to C++. My programs are simplistic, console-based programs. I've written in C++ before just to try & stay current. However, at my level, C++ seems unnecessarily complex without a proportionate gain over C. Could just be showing my ignorance lol. Thanks for the input. |
All times are GMT -5. The time now is 07:52 PM. |