Thread: About overloaded constructor

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    About overloaded constructor

    Hi guys,

    I want to make a program but I wanted to know that when should we use overloaded constructor?


    Code:
    class Vehicle
    
    {
    
     private:
    
     string Name;
     int reg_no;
     
    public:
    
     Vehicle()
     void getName();
     void setName();
      
    ~Vehicle() 
    
    };
    The default constructor can do the job?

    The default constructor automatically initializes an object which is created ,so what is the use of overloaded constructor?

    Also have I written the constructor correctly?



    I hope I am clear

    Thanks
    Last edited by student111; 06-12-2012 at 02:47 AM. Reason: adding more

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by student111
    I want to make a program but I wanted to know that when should we use overloaded constructor?
    When it makes sense

    Quote Originally Posted by student111
    The default constructor can do the job?
    Possibly. But perhaps it may make sense to create a constructor that can initialise the Name with a sensible value. In fact, if each Vehicle must have a Name that is not an empty string, it may even make sense to not provide a default constructor, and check in setName that the argument is not an empty string (of course, setName must take an argument).

    Quote Originally Posted by student111
    Also have I written the constructor correctly?
    You forgot the terminating semi-colon, and you did not actually define the constructor in the example code. Besides what I highlighted about setName, getName should be a const member function that returns a std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It makes sense to overload a constructor if parameters of differing types can potentially used to create a whole object.

    Let's say int makes sense to construct an object from an integral value in some circumstances, and from a string of characters in other circumstances. Generally, it would then make sense to create two constructors, one that accepts an int, and one that accepts a const char *. I've illustrated this using constructors that only accept one argument, but the principle works if you need to construct objects from multiple items of data (i.e. multiple parameters to the constructor).

    I don't know whether it makes sense in your Vehicle class. In what ways does it make sense to construct a Vehicle?

    I wouldn't supply a default constructor for a class, unless it makes sense to create an object based on nothing (no parameters required).

    Don't read on unless you like technical pedantry. Technically, unless copy constructors (that create an object from an existing object of the same type) are disabled, all constructors are overloaded. The compiler will generate a copy constructor if the programmer doesn't, and it can. It doesn't make sense to have a copy constructor unless there is some other way to create an object to be copied - which means another overloaded constructor must exist. The only exceptions to this are if copy constructors are specifically suppressed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Or perhaps if you had some kind of matrix class that by default created an 8x8 array when constructed. If you only actually wanted a 3x4 array, or you wanted a 30x40 array, then using a constructor that allowed you to provide the height and width would be an advantage over allocating an 8x8 array first then throwing that away when you call some kind of resize function, and allocating a differently sized one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    The default constructor automatically initializes an object which is created ,so what is the use of overloaded constructor?
    Default constructor does not initialize primitive types (POD). Even though default constructor exists for POD's but compiler don't call them unlike it does for object members.
    Code:
    Sample a; //Default or NO construction
    Sample b; = Sample(); //construction with data member value initialization
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by maven View Post
    Default constructor does not initialize primitive types (POD). Even though default constructor exists for POD's but compiler don't call them unlike it does for object members.
    That is not true.

    The definition of POD forbids user-declared constructors. However, compiler generated default and copy constructors are permitted and, if they are generated, they will be invoked when default-constructing or copy-constructing an object, respectively.

    C++-11 relaxed a lot of the rules related to POD (and introduced more specific concepts like trivial and standard-layout classes) but this principle still holds.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Great!! I was not aware of it. But people must have to use compiler which supports c++11 to get the advantages.
    S_ccess is waiting for u. Go Ahead, put u there.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by maven View Post
    Great!! I was not aware of it. But people must have to use compiler which supports c++11 to get the advantages.
    This was not the point. Ordinarily, POD types aren't constructed at all. But the compiler can generate constructors for them, and you are allowed to use them.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    class point 
    {
       private:
          float _x;
          float _y;
       public:
          point() : _x(), _y() {}
          
          string toString() const
          {
              stringstream result;
              result << '(' << _x << ',' << _y << ')';
              return result.str();
          }
    };
    
    int main ()
    {
       point origin;
       cout << origin.toString() << endl; // (0,0)
       return 0;
    }
    C++11 support is not necessary.
    Last edited by whiteflags; 06-13-2012 at 10:10 AM.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    My context was for implicit default constructors.
    I said "Default constructor does not INITIALIZE primitive types (POD)". This states true except when object is in static or global scope.
    Initialization is a different process performed while creating an object.
    So the fact remains is implicit default constructor do not initialize primitive types (POD) for a object which is created in a local scope.
    S_ccess is waiting for u. Go Ahead, put u there.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by maven View Post
    My context was for implicit default constructors.
    I said "Default constructor does not INITIALIZE primitive types (POD)". This states true except when object is in static or global scope.
    Initialization is a different process performed while creating an object. So the fact remains is implicit default constructor do not initialize primitive types (POD) for a object which is created in a local scope.
    The reason we had this discussion is because a default constructor is just a constructor that can be called without any arguments. If discussion was productive, you already know why that's wrong. Now that you've added context I've got no problem, compiler generated constructors are worthless
    Last edited by whiteflags; 06-14-2012 at 10:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help - overloaded constructor taking char or int
    By DHart07 in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2010, 02:57 AM
  2. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  3. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  4. Replies: 6
    Last Post: 05-19-2010, 04:03 AM
  5. Replies: 10
    Last Post: 06-02-2008, 08:09 AM