Need help in understanding below code

Discussion in 'C++' started by jagan, Jun 18, 2012.

  1. jagan

    jagan Guest

    Hi All,
    I am trying to learn c++. I studied that construct does not
    return anything and can be used
    to intialise only objects data memebers. Could someone explain how
    following is code is compiling
    in that case.

    #include <iostream>
    using namespace std;

    class complx {
    double re, im;
    public:

    // copy constructor
    complx(const complx& c) { re = c.re; im = c.im; }

    // constructor with default trailing argument
    complx( double r, double i = 0.0) { re = r; im = i; }

    };



    int main() {

    complx three = complx(3,4); // Here intialising object by calling
    constructor. How three is getting intialised
    // If constructor is not
    anything.
    }


    thanks
     
    jagan, Jun 18, 2012
    #1
    1. Advertising

  2. jagan <> wrote:
    > Hi All,
    > I am trying to learn c++. I studied that construct does not
    > return anything and can be used
    > to intialise only objects data memebers. Could someone explain how
    > following is code is compiling
    > in that case.
    >
    > #include <iostream>
    > using namespace std;
    >
    > class complx {
    > double re, im;
    > public:
    >
    > // copy constructor
    > complx(const complx& c) { re = c.re; im = c.im; }
    >
    > // constructor with default trailing argument
    > complx( double r, double i = 0.0) { re = r; im = i; }
    >
    > };
    >
    >
    >
    > int main() {
    >
    > complx three = complx(3,4); // Here intialising object by calling
    > constructor. How three is getting intialised
    > // If constructor is not
    > anything.
    > }
    >
    >
    > thanks


    The usual way of declaring and initializing a variable in C++ is not:
    complx three = complx(3,4);
    but rather:
    complx three(3,4); // this declares a variable 'three' and
    // calls the constructor on it

    complx(3,4) // this declares an anonymous temporary variable of
    // type 'complex' and calls the constructor on it

    The word 'complex' in all those expressions refers to the type 'complex'
    and not the name of the constructor. (The name of the constructor is
    actually 'complx::complx')
    The constructor is always called implicitly. The right constructor is
    selected by looking at the arguments.

    Additionally, in C++ the following two statements are considered the same:
    complx three = complx(3,4);
    complx three(complx(3,4));
    So, what you do is actually the following:
    - declare an anonymous temporary variable of type 'complx'.
    - this variable is initialized by calling the constructor
    complx::complx(double r, double i)
    - declare a variable called 'three' of type 'complx'
    - initialize the variable by calling the copy constructor with the
    anonymous variable from before.

    I hope this helps
    Tobi
     
    Tobias Müller, Jun 18, 2012
    #2
    1. Advertising

  3. On 6/18/2012 10:30 AM, jagan wrote:
    > Hi All,
    > I am trying to learn c++. I studied that construct does not


    Not "construct", but "constructor". It's a special function that is
    called by the program every time an object of that type is created.

    > return anything


    It's not entirely correct to say that it doesn't return anything. It
    has no specified return value type, yes. It is more proper to say that
    a constructor returns the new object (although it doesn't need to have a
    statement 'return <blah>').

    > and can be used
    > to intialise only objects data memebers.


    Well, if the constructor is defined for the class, it's *the only* place
    where data members are initialized (or not).

    > Could someone explain how
    > following is code is compiling
    > in that case.


    In the following code what you describe as "initializing an object by
    calling constructor", is in fact initializing an object *from another,
    temporary object". The expression 'complx(3,4)' creates a temporary
    object (without giving it a name), and then uses that object to
    initialize your named object 'three'.

    The object 'three' is initialized using the copy constructor, from the
    temporary. It's *almost the same* as writing

    complx three(3,4);

    but not quite.

    What book are you reading to learn C++? How carefully did you study the
    constructors and initialization chapter? Perhaps you need to read a
    better book, or at least go back to the one you have already read...

    Also, see section 10 of the FAQ.

    >
    > #include<iostream>
    > using namespace std;
    >
    > class complx {
    > double re, im;
    > public:
    >
    > // copy constructor
    > complx(const complx& c) { re = c.re; im = c.im; }
    >
    > // constructor with default trailing argument
    > complx( double r, double i = 0.0) { re = r; im = i; }
    >
    > };
    >
    >
    >
    > int main() {
    >
    > complx three = complx(3,4); // Here intialising object by calling
    > constructor. How three is getting intialised
    > // If constructor is not
    > anything.
    > }
    >
    >
    > thanks


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 18, 2012
    #3
  4. jagan

    Old Wolf Guest

    On Jun 19, 2:30 am, jagan <> wrote:
    >   complx three = complx(3,4);


    What this syntax actually does is:
    complx three( complx(3,4) );

    In English, it constructs a temporary complx(3,4),
    and then uses that to copy-construct "three". Your
    compiler might optimize this process.

    The "=" sign in a declaration has a different
    meaning to the "=" in an assignment.
     
    Old Wolf, Jul 9, 2012
    #4
  5. jagan

    Guest

    On Monday, June 18, 2012 7:30:00 AM UTC-7, jagan wrote:
    > class complx {
    > double re, im;

    These two data members are implicitly private, in case you didn't know.
    > public:
    > // copy constructor
    > complx(const complx& c) { re = c.re; im = c.im; }

    This allows me to do this:
    complx a(3.2, 2.9); //creates a new complx object named a
    complx b(a); //calls copy constructor of b
    `b`'s default constructor created the two members `re` and `im`. Then the copy constructor assigned to `re` and `im` the values of those in `a`.
    > // constructor with default trailing argument
    > complx( double r, double i = 0.0) { re = r; im = i; }

    This simply initializes the private data members to the values you pass to it. `i` has a default value, which means you can use `complx` this way:
    complx c(3.2);
    And `c.im` would have the value of 0.0.
    > };
     
    , Jul 9, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    292
  2. joshc
    Replies:
    14
    Views:
    800
    Keith Thompson
    Jan 14, 2005
  3. Ramesh
    Replies:
    7
    Views:
    152
    slebetman
    Apr 2, 2009
  4. kiran
    Replies:
    12
    Views:
    1,197
    Scott Sauyet
    Dec 7, 2011
  5. mnishpsyched

    Need help to sort out the below code...

    mnishpsyched, Sep 14, 2013, in forum: Python
    Replies:
    3
    Views:
    130
    John Gordon
    Sep 14, 2013
Loading...

Share This Page