1. Why is the const keyword used? How does it help enforce the \'\'principle of
ID: 645651 • Letter: 1
Question
1. Why is the const keyword used? How does it help enforce the ''principle of least privilege''? What is ''member initialize? syntax? Define and explain in an example. 2. Please explain the term ''composition'' in detail. 3. When and why are friend functions and classes utilized? List some advantages of using these object types. 4. What are some of the uses of the this pointer? 5. Why would you use a static data member? Can you use the this pointer or const keyword with a static member? Please explain. Give an example of a static declaration and explain how it is being utilized. 6. Explain data abstraction and its relation to information hiding.Explanation / Answer
1.The const keyword is used to create a read only variable. Once initialised, the value of the variable cannot be changed but can be used just like any other variable.
eg const pi=3.14;
Declaring an object as const helps enforce the principle of least privilege. Attempts to modify the object are caught at compile time rather than causing execution-time errors.
There are couple of ways to initialize the const members inside the class..
Definition of const member in general, needs initialization of the variable too..
static const int a =10;//at declaration
2. In order to facilitate the building of complex classes from simpler ones, C++ allows us to do object composition in a very simple way
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.