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

Give reasonably brief, but complete answers to the following. 7. When a function

ID: 3917962 • Letter: G

Question

 Give reasonably brief, but complete answers to the following.
 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 are the advantages of 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

Q7) 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

Make that object argument in the function signature as final so that the function doesnt modify that object parameter by mistake, because final prevents the values being passed from modifying and this will ensure that the object remains unchanged.

eg:-

public String doNotChangeMe(final String inputString){

return inputString;

}

It means that the member function can be called on a const object (or via a reference-or-pointer-to-const), and that the member function cannot modify data members (unless they're marked mutable ).

b) What are the advantages of defining a member function as 'const'?

It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided

Q9 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 constructor?

Because in C++ we cannot assign one object to another directly using assignment operator unlike in Java: eg:- Point p1; Point p2; p1 = p2; // this is invalid in C++.

If we want to initialize the data members of a class which has object as data members, in such cases we need to go for Initializer List because using this syntax whatever values that is captured in the constructor will get copied to the data members inside the constructor body. So, constructor duty is fulfilled.

#include<iostream>

using namespace std;

class Point {

private:

int x;

int y;

public:

Point(int i = 0, int j = 0):x(i), y(j) {}

/* The above use of Initializer list is optional as the

constructor can also be written as:

Point(int i = 0, int j = 0) {

x = i;

y = j;

}

*/

int getX() const {return x;}

int getY() const {return y;}

};

int main() {

Point t1(10, 15);

cout<<"x = "<<t1.getX()<<", ";

cout<<"y = "<<t1.getY();

return 0;

}

Please let me know in case of any clarifications required. Thanks!

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