LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2022, 09:17 AM   #1
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.2_64
Posts: 345

Rep: Reputation: 54
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.
 
Old 11-11-2022, 09:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,982

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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
{
    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;
}
 
1 members found this post helpful.
Old 11-11-2022, 09:54 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
https://www.linuxquestions.org/quest...gs-4175464257/
 
Old 11-11-2022, 10:46 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 11-11-2022, 11:18 AM   #5
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.2_64
Posts: 345

Original Poster
Rep: Reputation: 54
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.
 
Old 11-12-2022, 10:46 AM   #6
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,000

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
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
 
Old 11-12-2022, 11:03 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by Ace Blackwell View Post
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.

Last edited by dugan; 11-12-2022 at 11:13 AM.
 
Old 11-14-2022, 04:02 PM   #8
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.2_64
Posts: 345

Original Poster
Rep: Reputation: 54
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.
 
  


Reply

Tags
c++ programming, classes



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can you tell that someone is bad? or Good ? someone is bad? or Good ? abrenar General 10 02-24-2009 02:42 PM
Can someone tell me why my graphics isn't getting detected? phiktion Linux - Software 1 08-06-2006 11:03 AM
Can someone tell me why my graphics is getting detected? phiktion Linux - Software 2 08-06-2006 08:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration