Home › Forums › Programming › help with constructors
- This topic has 5 replies, 3 voices, and was last updated 14 years, 9 months ago by Anonymous.
-
AuthorPosts
-
-
21/04/2010 at 4:01 pm #7702AnonymousInactive
[code:1:4fbd8b9f94]class complex
{
private:
float real;
flaot imag;public:
//using 2 default perameters can also have 3 different constructors
//with 0,1,2 parameterscomplex(float = 1, float = 0);
//copy constructor
complex(const complex&);[/code:1:4fbd8b9f94]complex(float = 1, float = 0); is this line a constructor??
complex(const complex&); is this line also another constructor but is called copy constructor?? what exactly is it for??any help is appreciated :D
-
21/04/2010 at 4:23 pm #45663AnonymousInactive
[code:1:9504b05a2a]class complex
{
private:
float real;
flaot imag;public:
//using 2 default perameters can also have 3 different constructors
//with 0,1,2 parameterscomplex(float = 1, float = 0);
//copy constructor
complex(const complex&);[/code:1:9504b05a2a]complex(float = 1, float = 0); is this line a constructor??
complex(const complex&); is this line also another constructor but is called copy constructor?? what exactly is it for??any help is appreciated :D[/quote:9504b05a2a]
Yep pretty much, although typically the first should look like:
complex(float fReal, float fImaginary)If you want to initalize your members to the values specified you might do an initialization list constructor or normal constructor init ( the former is slightly more efficient).
Initilization list version’s:
With parameters:
complex(float fReal, float fImaginary):
real(fReal),
imag(fImaginary){}default:
complex():
real(1.0),
imag(0.0)As for copy the copy constructor it is used to copy the contents of one object to another i.e
complex a;
complex b = a;Which is essentially telling it to copy all the member variable values from a to b. ( i.e. a deep copy of the object).
copy constructor for this would be as follows:
complex(const complex& var)
{
real = var.real;
imag = var.imag;}
-
21/04/2010 at 6:00 pm #45665AnonymousInactive
copy constructor for this would be as follows: complex(const complex& var) { real = var.real; imag = var.imag; }[/quote:2296b0a6cd] so the reason that var can access the private attribue is because of complex&???
-
21/04/2010 at 7:11 pm #45667AnonymousInactive
so the reason that var can access the private attribue is because of
complex&???[/quote:cebfa15bf2]Pretty much. I can see the possible confusion because of what you probably know about encapsulation – private, public, protected access modifiers etc. But if you think of those access modifiers are relating to the class implementation and that a copy constructor is part of that class implementation (i.e. its defined in the same header/source file) then it can access private data in the complex class no problem.
And if the code you posted is your own, it’d be better to uppercase the first letter of your classes where possible, i.e. Complex instead of complex.
-
23/04/2010 at 12:32 pm #45675AnonymousInactive
hmm….but is the line:
[code:1:a9529b5902]complex(float = 1, float = 0); [/code:1:a9529b5902] a default constructor??because later on in the .cpp file, another constructor is declared
[code:1:a9529b5902]Complex::Complex(float r, float i){
real = r;
imag = i;
}[/code:1:a9529b5902]Are the parameters here would be set by the user instead of the compiler??
sorry about all this nooby question, I’m like very new to this C++ thing, although I’ve done a bit of C last year -
23/04/2010 at 1:05 pm #45676AnonymousInactive
its ok i found out now, thanks alot for the help peter_b and anthony
-
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.