difference between private protected and public in c++ code example

Example 1: private and protected in c++

protected://means all sub classes and base class can access these functions and variables butcan't be accessed outside classes
public://Pubic methods and variables can be accessed inside and outside of the class
private://only entity class can read and write the variables exeption is friend

Example 2: private and protected in c++

class A 
{
public:
    int x;
protected:
    int y;
private:
    int z;
};
 
class B : public A
{
    // x is public
    // y is protected
    // z is not accessible from B
};
 
class C : protected A
{
    // x is protected
    // y is protected
    // z is not accessible from C
};
 
class D : private A    // 'private' is default for classes
{
    // x is private
    // y is private
    // z is not accessible from D
};

Tags:

Cpp Example