Thread: Class Definition?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    Class Definition?

    In a class definition, what line of code would create a default constructor to initialize the two parameters (fltNum and fltMiles) of class Flight to 0 for both values?

    Code:
    class Flight
    {
    
    // The default constructor
    Flight() {
    fltNum = fltMiles = 0;
    };
    
    //...
    
    }; // end class Flight
    Any insights on what I think it is…

    Thanks

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Code:
    class Flight
    {
         public: 
              // The default constructor
              Flight() {
                   fltNum = fltMiles = 0;
              };
              //...
    }; // end class Flight
    edit: Or perhaps the default constructor doesn't need to be public. I'm not sure... I always make it public at least.
    Last edited by confuted; 08-07-2003 at 09:58 PM.
    Away.

  3. #3
    This sounds like homework. So a little hint:

    >>In a class definition, what line of code would create a default constructor

    The default constructor for a class named "Flight" must be named "Flight", and must not be passed any arguments. You already have such a constructor defined.

    >>to initialize the two parameters (fltNum and fltMiles)

    The constructor is just like any other function, except that it is called automatically when an instance of the class is created. You already have the lines inside the constructor that will initialise them both to 0.

    If it didnt look like it was formatted by a blind monkey, that might help too...

    Code:
    class Flight
    {
    public: 
       // The default constructor
       Flight()
       {
          fltNum = fltMiles = 0;
       };
    
       //...
    
    }; // end class Flight
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by lightatdawn

    The default constructor for a class named "Flight" must be named "Flight", and must not be passed any arguments.
    Not true. This is also a default constructor, which is passed two arguments:

    Code:
    class Point{
    public:
       Point(int x = 0, int y = 0);
    /* Rest of class */
    };
    A default constructor either takes no arguments, or supplies default values for every argument.

  5. #5
    >>A default constructor either takes no arguments, or supplies default values for every argument.

    True. My bad.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class definition error
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2009, 11:26 AM
  2. help with class varialbe definition..
    By smartalco in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2006, 03:37 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM