Welcome to the VirtuQ Forums.
Results 1 to 10 of 10
  1. #1

    Constructor question

    Sir,
    1) Why it is illegal for a constructor to have a return type?
    2) Can it perform any type of operations ?
    Please elaborate the concept for using them.

    Thanks
    Last edited by anon10110; 07-08-2012 at 09:55 PM.

  2. #2
    VirtuQ™ Moderator
    Join Date
    Jul 2011
    Location
    Bangalore, India
    Posts
    1,044
    Blog Entries
    2
    Anon10110,

    A constructor will specifically return the object of the class for which it is a constructor and hence there is no reason for it to have a return type. Almost any code can appears in the constructor, but (2) is very open ended. If you have a specific example, please put it here.

    Thanks,

    Anup

  3. #3
    Member
    Join Date
    Oct 2011
    Location
    Allahabad, India
    Posts
    245
    Anup,

    Take a look on the code given below, I am unable to understand what's really going on constructor part !

    #include <iostream>
    using namespace std;
    class MyClass
    {
    public:
    MyClass() : varI(0),varF(0) {}
    MyClass(int i,float f): varI(i),varF(f) {}
    int getVarI() { return varI; }
    float getVarF() { return varF; }
    void setVarI(int i) {varI=i;}
    void setVarF(float f) {varF=f;}
    private:
    int varI;
    float varF;
    };

    Thanks,

    Anon10020
    Anon 10020

  4. #4
    VirtuQ™ Moderator
    Join Date
    Jul 2011
    Location
    Bangalore, India
    Posts
    1,044
    Blog Entries
    2
    Anon10020,

    varI and varF are two variables whose values are being initialized to zeroes. In the second case you are initializing them with the values in 'i' and 'f'.

    Is that what you wanted to know?

    Thanks,

    Anup

  5. #5
    Member
    Join Date
    Oct 2011
    Location
    Allahabad, India
    Posts
    245
    Anup,

    Yes, exactly. But why they are initialized in this way ?
    MyClass() : varI(0), varF(0){}

    They can be defined in this way also,
    MyClass(){
    varI = 0;
    varF = 0;
    }

    Are the above both methods same?

    Thanks,

    Anon10020
    Last edited by anon10020; 24-10-2012 at 05:47 PM.
    Anon 10020

  6. #6
    VirtuQ Moderator
    Join Date
    Jun 2011
    Location
    Bangalore, India
    Posts
    310
    Blog Entries
    4
    Anon10020,

    In your case, end result would be same. One thing to note is that all the initialization which comes before '{' will be executed first before any statement in '{' and '}'.

    -- Basant

  7. #7
    Member
    Join Date
    Oct 2011
    Location
    Allahabad, India
    Posts
    245
    Basant and Anup,

    Thanks. I got it.

    Thanks,
    Anon10020
    Anon 10020

  8. #8
    Sir,
    How can I pass values to a constructor as a vector of integers?

    Thank you
    Last edited by anup; 28-02-2013 at 05:37 AM.

  9. #9
    VirtuQ™ Moderator
    Join Date
    Jul 2011
    Location
    Bangalore, India
    Posts
    1,044
    Blog Entries
    2
    Anon10440,

    Please do not put double question marks of multiple full-stops in questions. You can simply pass a vector to a constructor, what is the issue in doing that?

    Thanks,

    Anup

  10. #10


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •