// Class members (data & methods) can be public/private/protected. // The default is private. // // public: anyone can access them // private: nobody except other methods of this class can access them // protected: can be accessed by this class, and by immediately derived class // // There is also public/private/protected inheritance. // The default is private. // // public: public members in base class are my public members // protected members in base class are my private members // // private: public & protected members in base class are my private members // // protected: public & protected members in base class are my protected members class Base { // A base class, with a variable of each type public: int pub; // I’m public! private: int priv; // I’m private! protected: int prot; // I’m a little teapot, short & stout. }; class D1 : public Base { public: int foo1() { return pub; } // int foo2() { return priv; } // Sorry, but Base::priv is private int foo3() { return prot; } }; class D2 : private Base { public: int foo1() { return pub; } // int foo2() { return priv; } // Sorry, but Base::priv is private int foo3() { return prot; } }; class D3 : protected Base { public: int foo1() { return pub; } // int foo2() { return priv; } // Sorry, but Base::priv is private int foo3() { return prot; } }; int main() { Base b; // return b.priv; // Sorry, but Base::priv is private // return b.prot; // Sorry, but Base::prot is protected return b.pub; D1 d1; return d1.pub; // return d1.priv; // Sorry, but D1::priv is private // return d1.prot; // Sorry, but D1::prot is private D2 d2; // return d2.pub; // Sorry, but D2::prot is private // return d2.priv; // Sorry, but D2::priv is private // return d2.prot; // Sorry, but D2::prot is private D3 d3; // return d3.pub; // Sorry, but D3::pub is protected // return d3.priv; // Sorry, but D3::priv is protected // return d3.prot; // Sorry, but D3::prot is protected }