Well I've written a ton of C++ tutorials one of them being on Templated Classes here:
http://villavu.com/forum/showthread.php?t=82610Now don't bother trying to understand it if you are just starting out as it's going to probably discourage you BUT lets start from the beginning.
There is no such thing as a "Private Class". The correct terminology is Private/Public/Protected Inheritance.
There is a reason we call it that.
One: Private members of a class can only be accessed by other members of the same class (Brothers/Sisters, No cousins or Friends).
Two: Public members can be accessed by anyone (functions, main, etc) and any class or object.
Three: Protected members can only be accessed by other class members OR by classes that are "Friend"/"Inherited".
Four: Friends. Friends can access any part of your class, private, public and protected members. So limit your friends.
Five: Classes cannot be public; Only members can. Thus any class can access your class/object automatically. This is why we set members public/private/protected.
Six: In Java it's something like Public Class B Extends A or whatever. In C++ it's Class A : B; That's called deriving/inheriting.
Seven: Nested classes can put an accessor on It's daughter/son class.
Example:
class A
{
protected:
class B
{
};
};
I believe that's the only way to simulate what I think you're thinking..
Now why do we make things private? well we don't want the user to be able to change values of private members from outside the class. Here we can force them to use a Getter/Setter instead.
Example: Sometimes our class may have a pointer member or an internal member that will hold data. Other members/data may depend on the values that the private member holds. Now if the private member was public, and a user changed it, it can mess up the other members/functions.
Code Example:
class TBitmap
{
private:
RGB *ArrayOfPixels;
public:
TBitmap(int Width, int Height); //Constructor for our class.
~TBitmap(); //Destructor.
void SetPixels(int Index, COLORREF Colour); //This is known as a setter function.
};
TBitmap::TBitmap(int Width, int Height) : Pixels(0) //Implementation of the constructor.
{
//Create A Blank Bitmap here.
ArrayOfPixels = new RGB[Width * Height]; //Every new[] allocation must have a delete[] clearance.
}
TBitmap::~TBitmap() //Implementation of the destructor.
{
delete[] ArrayOfPixels; //We MUST Cleanup after ourselves.
}
TBitmap::SetPixels(int Index, COLORREF Colour)
{
ArrayOfPixels[Index] = Colour; //Our user never gets to touch the array. We let a function do the work.
//Thus the user accesses this indirectly!
}
int main()
{
//Let us ASSUME that ArrayOfPixels was public ok?
TBitmap SomeBMP(100, 100); //Create a bitmap of 100 x 100 pixels.
delete[] SomeBMP.ArrayOfPixels; //DANGEROUS!! We deleted our array from outside the class.
//Our program ends here BUT it doesn't just end, it will crash or cause undefined behaviour!
//WHY? Because we already deleted ArrayOfPixels and our Destructor is about to get called to delete something that isn't there since we already deleted it!!!!
//For this reason it was better to leave ArrayOfPixels as private OR use a MoveConstructor/CopyConstructor/C++11 Semantices.
//Also for this same reason is why we access it through the Setter functions so the user can't bullshit us. We control how they use our class!
}
Read the comments in the above code. As you can see, it CAN be dangerous to expose some members as public without the proper semantics.
By Default, All class members are private and all Struct Members are public. So inother words a Struct is what you are calling a public class and a class is what you are calling a private struct.
Both are the same thing with different default inheritance. Think from the user standpoint when creating a class such as the above. We don't want users doing stupid things such as touching and deleting things they shouldn't.
Anyway here is someone asking the same thing as you but with more beginner friendly answers:
http://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-cI would recommend that you read that to see the differences between C++ and Java/C# class inheritance.