Unit_8: Encapsulation Flashcards

1
Q

Explain public, private protected.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

compare Private vs Protected

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Unique C++ features? Why need it?

A

friend

allows a function, class, or method to access private and protected members of another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why protected (in Java) insecure?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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.

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

c) Graph can access Node’s private methods and data members. (Slide 26: Friendship allows full access to private members.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly