Home Forums Programming help with constructors

Welcome to our forums. These forums were active from 2003-2014. We have now decided to close them down, but will leave them here as an archive.

Remember you can send us feedback, news, jobs and content ideas by clicking here.

If you're really stuck for time, email news@gamedevelopers.ie.

You can also follow us on Twitter @gamedev_ie 

 

 

This topic contains 5 replies, has 3 voices, and was last updated by  jinkazama99 7 years, 4 months ago.

  • Author
    Posts
  • #7702

    jinkazama99
    Participant

    [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 parameters

    complex(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

  • #45663

    peter_b
    Participant

    [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 parameters

    complex(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;

    }

  • #45665

    jinkazama99
    Participant
    
    
    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&???
  • #45667

    anthony
    Participant

    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.

    @anthonycosgrave

  • #45675

    jinkazama99
    Participant

    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

  • #45676

    jinkazama99
    Participant

    its ok i found out now, thanks alot for the help peter_b and anthony

The forum ‘Programming’ is closed to new topics and replies.