Accessing Classes in Classes

This is a discussion on Accessing Classes in Classes within the C++ Programming forums, part of the General Programming Boards category; Originally posted by GrNxxDaY this can be summed up in 1 word: WHAT? The colon : is used in the ...

  1. #16
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by GrNxxDaY
    this can be summed up in 1 word: WHAT?
    The colon : is used in the member initialization list which is what
    Code:
    Date(int _day = 0, int _month = 0, int _year = 0): day(_day), month(_month), year(_year){}
    is... day is initialized to _day, month to _month, etc.... look it up for the reasons why you would want to use this

  2. #17
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by GrNxxDaY
    i dont know what protected means, so im gonna ignore it and hope i never need it
    ROTFL!

  3. #18
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Why not protected?

    That would only help if he was attempting to access the private data from a derived class. But I don't think he has quite made it to inheritance yet.

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    Also, is it necessary to have that setdate member function?

    There will be times when you'll want to change an existing date, right? That's what it's there for, plus:

    why not let the Date constructor do it?


    Because the Date constructor is called BEFORE you can manipulate it! That's just a weird quirk that will make more sense in the future. To sum it up, look at this:


    Date day;

    int main() //...'day' constructor occurs here!!
    {
    day(10, 5, 99); //...too late!...'day' constuctor already called!


    return 0;
    }


    Likewise, when you have a class nested in another class, the contructor is invoked before you even reach the larger classes constructor! This is just the way it is, but as I said, you'll usually need a "SetDate()" type function in classes anyway, so why worry?

    Incidentally, a lot of professional programmers call the "Set()" function within the constructor! So instead of seeing:


    Code:
    Date(int _day = 0, int _month = 0, int _year = 0): day(_day), month(_month), year(_year){}
    You'll see:

    Code:
    Date(int _day = 0, int _month = 0, int _year = 0){ SetDate(_day, _month, _year); }  //...!

    Well I hope I didn't confuse things!
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing classes
    By Mavix in forum C# Programming
    Replies: 2
    Last Post: 02-16-2008, 08:43 PM
  2. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  3. accessing classes and reversing inputs
    By 2fastwrx in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2004, 08:16 AM
  4. 2 Classes Accessing Eachother
    By Chronom1 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 05:10 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM

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