Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7. When a function has an object parameter that it doesn\'t modify, what is the

ID: 665793 • Letter: 7

Question

 7. When a function has an object parameter that it doesn't modify,    what is the best way to declare the parameter (in the function    signature), and why?          Show an example using a function with a string object parameter.  -------------------------------------------------------------------- 8. (a) When should a member function be declared as a 'const' member    function?      (b) What advantage is there to defining a member function as 'const'?  -------------------------------------------------------------------- 9.  a) When a class has objects as data members, why should its      constructors use initialization lists (member and initializer      syntax) vs. assignment to the members in the body of the ctor?          b) Name two cases where you *must* use initialization lists in     constructors (as opposed to assignment in the ctor body). 

Explanation / Answer

1.
To pass the parameter as 'const'
Example:
void test(const string str){
   cout << string << " ";
}

2.
(a) When it is not allowed to change any class members.
(b) It keeps tehe function from modifying things that shouldn't be modified.

3.
(c) For initialization of non-static const data members, for initialization of member objects which do not have default constructor or fr initialiation of base class members

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote