Can someone tell me what I'm not getting in this program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can someone tell me what I'm not getting in this program
All, being brief as possible, I知 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稚 sure what the exercise was asking. I found someone online who had done the exercises. I thought seeing the program in action, I壇 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知 hoping someone here can clear up what I知 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知 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/
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.
Last edited by sundialsvcs; 11-11-2022 at 10:51 AM.
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.
Move2::add returns a reference or address of a newly created Move class called newMove.
Uh, no it doesn't. It returns newMove by value.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.