-
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--------------------
-
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.
-
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]