[C++] why we have a constructor?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkkal
    New Member
    • Oct 2007
    • 2

    [C++] why we have a constructor?

    Im a student who is majoring in computer.
    in C++, why we have a constructor ?
    I think there is some reasons for Bjarne Stroustrup to make constructor in C++

    Thank you ^^
  • Firecore
    New Member
    • Jul 2007
    • 114

    #2
    Originally posted by darkkal
    Im a student who is majoring in computer.
    in C++, why we have a constructor ?
    I think there is some reasons for Bjarne Stroustrup to make constructor in C++

    Thank you ^^
    Mabye because it makes our lives easier so the class is initialised when its declared? I guess thats a good reason.

    That is actually a very good question.
    Im not being sarcastic.

    Comment

    • Meetee
      Recognized Expert Contributor
      • Dec 2006
      • 928

      #3
      Originally posted by darkkal
      Im a student who is majoring in computer.
      in C++, why we have a constructor ?
      I think there is some reasons for Bjarne Stroustrup to make constructor in C++

      Thank you ^^
      Hi,

      Have you search on google regarding this issue? Read http://www.cplusplus.com/doc/tutorial/classes.html for your answer.

      Regards

      Comment

      • Mayur2007
        New Member
        • Aug 2007
        • 67

        #4
        Originally posted by darkkal
        Im a student who is majoring in computer.
        in C++, why we have a constructor ?
        I think there is some reasons for Bjarne Stroustrup to make constructor in C++

        Thank you ^^
        Hi darkkal,

        In C++ there is a use of constructor.
        with the use of constructor u can define the variables value when the program runs. it will call automatically no need to call explicitly.
        when u want to use constructor just make the same name as your class. and define the values of the variable.
        this is the basic idea abt constructor.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Okay, imagine you created this robot. It cooks, it cleans, it plays video games. It's the best robot ever. However, when you created it, you didn't put any logic in it so that you could order it to do things, and when you turned it on, it decided to go become a member of a reggae band in Jamaica.

          Without a constructor, your code is off in Jamaica, unreachable. It is no use to you because you can't call it, you can't instantiate it, you can't even tell it exists. When you call a constructor, you finally create a modifiable instance of your class, and that's when you can use it and the methods it creates. Obviously, this is not a perfect analogy, there are exceptions to this, but that is the gist of it.

          Comment

          • Nkhosinathie
            New Member
            • May 2007
            • 91

            #6
            hi, i'm also doing c++ programming.my idea of a constructor is that it help you to initialisize private members in a class. because if you don't use it the compiler will implicitely do it for you.
            thanks.

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by Nkhosinathie
              hi, i'm also doing c++ programming.my idea of a constructor is that it help you to initialisize private members in a class. because if you don't use it the compiler will implicitely do it for you.
              thanks.
              Well, good practice would indicate that you also include get/set methods, but if you know the values on initialization it is a good idea to set those in the default constructor as well.

              Comment

              Working...