Thread: class?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Red face class?

    I have a few questions about class.

    - Does every class need to have "private" members? It seems that the private members are used in initialization. If no initialization is needed, then maybe they are not needed...

    - in this example
    class A
    {
    public:
    A()
    { cout << "A" << endl;
    }
    };

    class B
    {
    public:
    B()
    { cout << "B" << endl;
    }
    };

    class C
    {
    public:
    C()
    { cout << "C" << endl;
    }

    private:

    A myA; //can we use class as a data type?
    B myB;
    };

    int main ()

    {
    C c;
    }

    Why the output would be
    A
    B
    C

    ---------------------Thank you--------------------

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Initialization is done by a constructor for public, protected, and private members to insure the object has the correct initial state.

    Protected, private, and public are access modifiers. They set access to members and methods. Public, anyone can access it, protected, only derived classes can access it (except the class itself ). Private no one except the class itself can access it.

    Not every class will require a private members but if you need to encapsulate things that tightly it is availible.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Beginner?

    OK, if you were to just do this:

    class my_class
    {
    varibale_type myvariable1[3];
    }

    That variable would be private. If you do not label them, class members are private by default. But you could make the whole thing public if you want. C++ does automatically put it's own variables to deal with the calss (for example a pointer called *this, which you will learn about later if you are a beginner) in the private section. Everything you use can be public or protected if you want it to. struct is public by deault That's the only difference between the two.

    Sean Mackrory
    [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM