Class Definition?

This is a discussion on Class Definition? within the C++ Programming forums, part of the General Programming Boards category; In a class definition, what line of code would create a default constructor to initialize the two parameters (fltNum and ...

  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
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21