Need help in understanding below code

J

jagan

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
 
T

Tobias Müller

jagan said:
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
 
V

Victor Bazarov

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 said:
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
 
O

Old Wolf

  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.
 
R

richardturdman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,920
Messages
2,570,038
Members
46,449
Latest member
onedumbsquirrel

Latest Threads

Top