Concept of code structure Public vs Private

Hello fellow programmers,

I was wondering about why when you add a new C++ class, you get the choices of putting your class in a Public/Private or none at all, just include it in a solution.

I understand the concept of OOP public and private keywords. Private would make it unaccessible like a property or a method outside the class. But what’s the point of having a private class? And what would you put as a private class in the folder?

They are useful when a class does not want outside influences affecting certain variables or functions.

For example, take our magazine class.
It has a Count & Capacity.
Count should never be less then zero or greater then Capacity.

If Count was public, it would be possible for an outside class to set Count to a value outside of this range, resulting in Bad Things™ happening.

If we use private values combined with a public function, we can make sure the value we’re about to set is good before actually setting it:



bool AGBMagazine::AdjustCount(int32 Amount)
{
    if ((Count + Amount >= 0) && (Count + Amount <= Capacity))
    {
        Count += Amount;
        return true;
    }
    return false;
}


It’s also worth noting that getters and setters should only be created for things you actually expect to be accessed. A lot of people fall into the bad habit of creating getters/setters for everything when they first learn about them.

When you type MyClass::… you should see an intellisense drop-down that effectively tells someone everything they need to know about how to use your class. It’s a good habit to use this list to keep your class light and relevant.

They said they understand private variables. They are asking about private classes.

Indeed, as I said I’m aware of the OOP principles of why should a variable be private and needs a getter and setter. But what I’m trying to ask is why when you make a class via Add C++ code via the Editor you can choose to put it either in Public or Private folder

Public means the class will be exported and accessible from outside its Module. Private means it is only accessible from within the Module its defined in.

Don’t I feel silly. :frowning:

I’m sure the explanation will help someone googling around. :slight_smile:

@Kris: It surely will, you’ve done a greater example than most topics explaining that concept.
@ Oh that makes sense now. Thanks.