Newbie question about the Private type in classes

This is a discussion on Newbie question about the Private type in classes within the C++ Programming forums, part of the General Programming Boards category; Im really new, and you're all probably laughing by now . But, what is the point of the private access ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Newbie question about the Private type in classes

    Im really new, and you're all probably laughing by now .
    But, what is the point of the private access type in classes? Is that just there so YOU don't accidentally modify that, and the compiler gives you a kick upside the head if you do? What am I overlooking? Are they really that useless?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    class Object {

    private:

    int safest;

    protected:

    int safe;

    public:

    int exposed;

    };



    class NewObject : public Object {

    public:

    void manipulate()
    {
    exposed = 12; //..fine...
    safe = 2; //...fine. Derived classes can access "protected:"
    safest = 6; //...cannot do! private to "Object" class...
    }

    };


    int main()
    {

    Object ob;

    ob.safest = 10;//...no way...
    ob.safe = 12;//...nope
    ob.exposed = 13; //...no problem

    return 0;
    };



    Ok, so you knew all that, right? Just a refresher. The idea behind it all is "data-hiding". You can use it as a tool to remind yourself that you didn't want to be able to access a member of an objects class directly, (maybe there are butterfly effect implications) or similar reasons. In other realms of programming, this mechanism has helped software library authors control what goes in and out of a class instance, information-wise, providing a sort of controlled-component functionality. But there are many other possible reasons. The point is, unless it's necessary for you at the moment, don't bother. But when you DO need it, you'll be glad it there
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 02:14 AM
  2. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 02:21 PM
  3. Type conversion question.
    By ellis in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2006, 04:05 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM

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