Home Forums Programming help with constructors

  • This topic has 5 replies, 3 voices, and was last updated 14 years ago by Anonymous.
Viewing 5 reply threads
  • Author
    Posts
    • #7702
      Anonymous
      Inactive

      [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
      Anonymous
      Inactive

      [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
      Anonymous
      Inactive
      
      
      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
      Anonymous
      Inactive

      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.

    • #45675
      Anonymous
      Inactive

      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
      Anonymous
      Inactive

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

Viewing 5 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.