Unit_8: Encapsulation Flashcards
Explain public, private protected.
- public: visible to any user of a class X object
- private: visible only to X’s own member functions => better security and reliability, and better control of data locally
- protected: visible in X’s member functions and member
functions of subclasses (derived classes) of X
compare Private vs Protected
we try to isolate functionality in asuperclass – i.e., if the superclass owns a variable they should be defining ways to manipulate it
- This is a general principle you should always follow!
Subclasses should deal only with the things they
are responsible for
What is Unique C++ features? Why need it?
friend
allows a function, class, or method to access private and protected members of another class
Why protected (in Java) insecure?
protected members are accessible by subclasses AND classes in the same package
=> should be private, with
protected accessors/mutators that can be inherited by children
- Which of the following is FALSE about private inheritance in C++ (e.g., class A : private B)?
a) Public members of B become private in A.
b) Protected members of B become private in A.
c) A can access B’s protected members.
d) B’s public members are accessible to A’s subclasses.
e) Private inheritance restricts access to B’s members outside A.
d. because The status of the base class defines what the status of its public members are in the inherited class
=> Private inheritance makes all of B’s public and protected members private in A, so they are not accessible to A’s subclasses
In C++, what does the friend keyword allow when used in a class declaration (e.g., friend class Graph in Node)?
a) Graph can access Node’s private methods but not data members.
b) Node can access Graph’s private methods and data members.
c) Graph can access Node’s private methods and data members.
d) Node’s subclasses can access Graph’s private members.
e) Graph can only access Node’s public members.
c) Graph can access Node’s private methods and data members. (Slide 26: Friendship allows full access to private members.)