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

15) Given the following code: // ... CSC 122 Sections 1 & 2 Time: Due: T May 2 N

ID: 3831612 • Letter: 1

Question

15) Given the following code:

// ...

CSC 122 Sections 1 & 2

Time: Due: T May 2
Notes: ???

Calculator: OK

Form S — 3 of 9

DataStorer operator++(int) { DataStorer temp(data); ++data; return temp; } };

Explain what is going on in the code. In your discussion, be sure to answer the following questions: i) What’s that unnamed int argument?

ii) Why’s the function defined inside the class definition? iii) Does this class do anything useful?
iv) Is there anything wrong with this code?

Declare an object of this class type and show how a programmer would normally call the given operator.

16) The data type of a file stream is either ifstream or ofstream (defined in the library). However, when passing a file stream to a function, we normally use the data types and , respectively. This is because it provides compatibility with the console streams (letting the one function work with both and an input file or to work with both and an output file

depending on what actual argument is passed). However, when using the or
methods, the file stream must be passed through an ifstream or ofstream argument. (After all, who would want to do those actions to the console?!)

No matter which types you choose, streams must always be passed as arguments because no matter what you do to a stream, you are changing it in some way.

17) TRUE / FALSE Function object classes can contain data members to remember ’state’ between calls.

TRUE / FALSE Function object classes can overload operator() multiple times for different reasons.

TRUE / FALSE Function object classes passed to functions by template arguments may need to have mutable data members to work properly.

TRUE / FALSE A function object is so named because it is an object that can be used just as if it were a function.

Start Date: T Apr 25

Test 3 — [File] Stream Processing & CSC 122 Sections 1 & 2
Operator Overloading Time: Due: T May 2 Calculator: OK

Form S — 4 of 9

the user wants to exit, they can hit Control-C.) (Do not worry about overwrite vs append, either.) (What kind of loop would be most appropriate?) (Hint: What two things are commonly forgotten in a file opening loop?)

19) Show (pseudo)code for a ”read ’til end of file” loop. (What kind of loop would be most appropriate? Where is/are the reading statement(s)? Where is the processing of the read data?)

Does the stream involved in your eof loop have to be an input file stream or can it be cin?

20) Given the following Sum function object class:

Notes: ???
18) Show (pseudo)code for a common loop for opening an output file. (Do not allow fancy exiting conditions. If

class Sum {

Sum(double s = 0.0) : sum(s) { }
double operator() (double num) { return sum += num; } double operator() (void) const { return sum; }
double reset(double s = 0.0) { swap(sum, s); return s; }

// create
// update
// access
// [re]initialize

};

Show how to use it to calculate the average of the elements in the following array. (What kind of loop would be most appropriate?) (The declarations are normative; the initializations are illustrative.)

const size_t MAX = 50;
long a[MAX] = { 10, 14, 12, 8, 10, 6, 9, 13, 7, 11 }; size_t num_as = 10;

Explanation / Answer

DataStorer operator++(int) {
DataStorer temp(data);
++data;
return temp; } };
Answers:
1)The above function is used for postfix operator overloading which takes a dummy int, thats all the there is no purpose for int there.Its just a dummy int.
2)The function is defined under the class defiition because it makes it inline function even if you donot use theinline specifier.
3) Yes this class overloads the postfix operator and return the value.
4)You can use the following code in which the data is used to make a copy like
DataStorer operator++(int) {
DataStorer temp(*this);
++(*this);
return temp;
}
};

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