Thread: Implicit Constructor

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Angry Implicit Constructor

    I'm having trouble understanding these few sentances from a book that i'm reading. The sentace referers to this code:
    Code:
    // englobj.cpp
    // objects using English measurements
    #include <iostream.h>
    
    class Distance                    // English Distance class
       {
       private:
          int feet;
          float inches;
       public:
          void setdist(int ft, float in)   // set distance to args
    	 { feet = ft; inches = in; }
    
          void getdist()              // get length from user
    	 {
    	 cout << "\nEnter feet: ";  cin >> feet;
    	 cout << "Enter inches: ";  cin >> inches;
    	 }
    
          void showdist()             // display distance
    	 { cout << feet << "\'-" << inches << '\"'; }
       };
    
    void main()
       {
       Distance dist1, dist2;         // define two lengths
    
       dist1.setdist(11, 6.25);       // set dist1
    
       dist2.getdist();               // get dist2 from user
    
    				  // display lengths
       cout << "\ndist1 = ";  dist1.showdist();
       cout << "\ndist2 = ";  dist2.showdist();
       }
    "...However, we also want to define variables of type DISTANCE without initializing them, as we did in ENGLOBG.
    Distance dist1, dist2;
    In that program there was no constructor, but our definition worked just fine. How could they work without a constructor? Because an implicit constructor was built into the program automatically by the compiler and it created the objects, even thought we didn't define it in the class..."

    Can someone explain what hes' talking about?
    #include <Jesus.h>
    It will save your life

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Every class has a constructor. If u dont specify it the compiler will automatically create one for you.

    BY THE WAY. is this that Robert Lafore book object oriented programming with c++
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    ya it is, it's his second edition OOP. how'd you know?
    another quesiton. why does it need a constructor anyways?
    #include <Jesus.h>
    It will save your life

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    When you declare a class, the compiler supplies a default constructor automatically, but only as long as you don't define a constructor for the class yourself. A constructor is called when a new instance of a class is defined. It provides the opportunity to initialize the new object as it's created. The default constructor that's generated by the compiler does nothing--in particular, the default constructor does not initialize the data members of the object that's created, and therefore they will contain junk values.

    "why does it need a constructor anyways?"

    To create the object.

    You can always tell which function in a class is the constructor because it has the same name as the class and no return type.
    Last edited by 7stud; 04-22-2003 at 07:22 PM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    so does every data type in C++ need a constructor to create it. for example does
    int, float, double, long, struct types, classes
    all need a constructor supplied by the comiiler atutomatically to create them?
    #include <Jesus.h>
    It will save your life

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Blanket
    so does every data type in C++ need a constructor to create it. for example does
    int, float, double, long, struct types, classes
    all need a constructor supplied by the comiiler atutomatically to create them?
    only objects have constructors. the compiler will just allocate space for the primitive types (ie. int, float, etc...)

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    so does every data type in C++ need a constructor to create it
    Yes, both for user-defined and built-in (int, float, ...) object(s).

    For example explicit call of the constructor for int´s
    Code:
    int(5);
    "why does it need a constructor anyways?"
    To construct(create) object(s) and to initialize data-members in it.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    so does this mean you can add your own constructor to different data constructs, such as structures? and i don't understand why you need a constructor left blank ( cool { } ) when you declare your own constructors. Ive seen some class constructs that declare constructors but do not include cool { } (empty one) while others declare their own constructors but include the cool { }(empty one)
    #include <Jesus.h>
    It will save your life

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Blanket
    so does this mean you can add your own constructor to different data constructs, such as structures? and i don't understand why you need a constructor left blank ( cool { } ) when you declare your own constructors. Ive seen some class constructs that declare constructors but do not include cool { } (empty one) while others declare their own constructors but include the cool { }(empty one)
    you just confused me with that one.

    do you mean that there are constructors that don't do anything?
    yes, there are, and that is design decision. not really necessary.

    yes, you can add constructors to structs. in c++, structs and classes are the same except for the default specifier.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quoting one of the greats:

    "I recognize the words, but they don't make any sense."

    [edit]
    AhhhhHAH! Now, I think I understand. Reread this statement:

    "When you declare a class, the compiler supplies a default constructor automatically, but only as long as you don't define a constructor for the class yourself."

    Ok, so now we are getting somewhere. What if I define a constructor like this:
    Code:
    cool(int a, int b)
    {
        data_member1=a;
        data_member2=b;
    }
    Based on the statement above, now the compiler WON'T supply a default constructor. So, if I do this:

    cool a(3, 5);
    cool b;

    The second line will cause an error, because there is no constructor defined that takes no parameters. I didn't declare a default constructor, and the compiler doesn't supply one automatically if you define any constructor yourself, so there isn't a default constructor and the second line results in an-->>>ERROR.

    You can define many different constructors for your class, but if you define even one, then there won't be a default constructor, so if you want to create objects with a default constructor, then you have to define it yourself:

    cool ()
    { }

    If you don't want to create objects without initializing them in a constructor(i.e you don't want to create a default constructor), then don't define one.
    Last edited by 7stud; 04-23-2003 at 08:01 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There are also other reasons to define a default constructor. My beginning C++ book likes to demonstrate visually when a constructor is called, so it invariably defines a default constructor even though the compiler will supply one automatically. Why? Because my book adds some code:
    Code:
    cool()
    {
         cout<<"Default constructor called."<<endl;
    }
    Outputting a message when a constructor or destructor is called is a helpful way of showing when they've been called. Then, if you did something like this:

    cool my_array[10];

    You would see the default constructor message output to the screen 10 times as the constructor was called each time for the 10 objects in the array. Try it. If you also define a destructor like this:
    Code:
    ~cool()
    {
        cout<<"Destructor called."<<endl;
    }
    Then when your program ends, the destructor will automatically be called to destroy the array, and you'll see the destructor message ten times.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    hmmm..ok 7stud. thanks, you clarified it for me. yah my question was strange now that i read it again

    but coolio . thanks
    #include <Jesus.h>
    It will save your life

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    Is there any advantages, or reasons in declaring a function inside a class but defining it some place else in the listing. The only reason i see is defining it closer to the function call makes it easier for the programmer to read teh code.
    #include <Jesus.h>
    It will save your life

  14. #14
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    There is a reason for this. When a function is defined with its prototype inside the class, the function is inlined. This means that whenever the function is called, the call to the function is replaced by the code for the function. If the function is not defined in the class, then it _may_ be defined as a normal function (your compiler can still inline it if it wants to).

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you can also make it inline without having it defined in the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. constructors
    By shrivk in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 09:35 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. implicit constructor and class access
    By Clarinetster in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2001, 03:59 PM