In: Computer Science
In C++, the concept of a "bag" may be represented as (click all that apply):
a vector of pointers of a specific object type |
a vector of objects |
a vector of generic pointers |
an array of whole numbers |
The C++ keywords used to implement the principle of least privilege are (click all that apply):
public |
#include |
const |
struct |
ifndef |
class |
If a class Employee has a data member Date hireDate , and Employee's member functions need to access private data members of Date ... (click all that apply)
..Date has to declare the class Employee as its friend, or... |
...Employee has to declare Date as its friend, or... |
...trick question -- private members are accessible only by other members of the same class. That's the whole point of making members private. |
What is the output from the code block below:
int i = 8; cout << i++ << " "; cout << ++i << endl;
8 10 |
9 10 |
9 9 |
8 9 |
Answers:
1) vector of pointers of a specific object type and a vector of objects
Explanation:
A bag can have different objects and there can be any number of objects. These objects can be of same type, or of different types. A vector is a container that can store elements. Thus, the vector is the bag, and the objects or the pointers of a specific objects type are the items inside a bag.
2) const
Explanation:
const can be used with a data member or a function to make them constant. It provides a read only access over them, and they cannot be changed once defined as const. Thus, it is the keyword to implement the principle of least privilege.
3) Date has to declare the class Employee as its friend
Explanation:
Private data members of a class can be accessed only by other members of the same class. If another class wants to access them, they have to be declared as its friend by the class holding the private data members. In this case, Employee class wants to access the private data member hireDate of the Date class. So, in order to do that, Date class has to delcare Employee class as its friend.
4) 8 10
Explanation:
i++ is defined as post increment, i.e., the value of 'i' will be printed first, and then incremented. ++i is defined as pre-increment, i.e., the value of 'i' will be incremented first, and then printed. In this case, the value of 'i' which is 8, is printed first and then incremented to 9. Then in the next line, it is incremented to 10 first, and then printed. Thus, the output is 8 10.