Show Lecture.Access as a slide show.
public:
, private:
, or protected:
access.
public
on every member–that’s Java.
private
for class, public
for struct.
public
anyone can access them
private
only methods of this class can access them
protected
can be accessed by this class,
and by immediately derived class
public
member accessclass Foo { public: void za() { cout << "🍕"; } int n = 42; }; int main() { Foo f; f.za(); cout << f.n << '\n'; }
🍕42
A public
data member is rarely the right thing to do.
private
member accessclass Foo { private: void za() { cout << "🍕"; } int n = 42; }; int main() { Foo f; f.za(); cout << f.n << '\n'; }
c.cc:9: error: 'void Foo::za()' is private within this context c.cc:3: note: declared private here c.cc:10: error: 'int Foo::n' is private within this context c.cc:4: note: declared private here
Only Foo
can use private
members.
protected
member accessclass Foo { protected: void za() { cout << "🍕"; } int n = 42; }; int main() { Foo f; f.za(); cout << f.n << '\n'; }
c.cc:9: error: 'void Foo::za()' is protected within this context c.cc:3: note: declared protected here c.cc:10: error: 'int Foo::n' is protected within this context c.cc:4: note: declared protected here
Only Foo
and its immediately derived classes can use
protected
members.
public
, private
, or protected
inheritance.
public
inheritance
public
members in base class are my public
members
protected
members in base class are my private
members
private
inheritance
public
& protected
members in base class are
my private
members
protected
inheritance
public
& protected
members in base class are
my protected
members
private
for class, public
for struct.
public
inheritanceclass B { public: void pub() {} private: void priv() {} protected: void prot() {} }; class D : public B { public: D() { pub(); priv(); prot(); } }; int main() { D d; d.pub(); d.priv(); d.prot(); }
c.cc: In constructor 'D::D()': c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:17: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:18: error: 'void B::prot()' is protected within this context c.cc:4: note: declared protected here
private
inheritanceclass B { public: void pub() {} private: void priv() {} protected: void prot() {} }; class D : private B { public: D() { pub(); priv(); prot(); } }; int main() { D d; d.pub(); d.priv(); d.prot(); }
c.cc: In constructor 'D::D()': c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:16: error: 'void B::pub()' is inaccessible within this context c.cc:2: note: declared here c.cc:16: error: 'B' is not an accessible base of 'D' c.cc:17: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:17: error: 'B' is not an accessible base of 'D' c.cc:18: error: 'void B::prot()' is protected within this context c.cc:4: note: declared protected here c.cc:18: error: 'B' is not an accessible base of 'D'
protected
inheritanceclass B { public: void pub() {} private: void priv() {} protected: void prot() {} }; class D : protected B { public: D() { pub(); priv(); prot(); } }; int main() { D d; d.pub(); d.priv(); d.prot(); }
c.cc: In constructor 'D::D()': c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:10: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:16: error: 'void B::pub()' is inaccessible within this context c.cc:2: note: declared here c.cc:16: error: 'B' is not an accessible base of 'D' c.cc:17: error: 'void B::priv()' is private within this context c.cc:3: note: declared private here c.cc:17: error: 'B' is not an accessible base of 'D' c.cc:18: error: 'void B::prot()' is protected within this context c.cc:4: note: declared protected here c.cc:18: error: 'B' is not an accessible base of 'D'
public
inheritance private
inheritance private
.
It might be better to declare the base class as a private data
member, instead.
protected
inheritance