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

Given the following code: class DataStorer { short data: public: //..... DataSto

ID: 3857418 • Letter: G

Question

Given the following code: class DataStorer { short data: public: //..... 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. 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 depending on what actual argument is passed). However, when using ______ 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 a 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 TRUE/FALSE Function object classes can contain data members state 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 wore a function.

Explanation / Answer

15.

i) unnamed int is the value passed int the operator overloading function which actually tells what kind of increment we need to do i.e. postfix(i++) of prefix (++i). Here int says that it is prefix which is wrong because int always comes in postfix function... It is basically to differentiate two types of increment overloading functions.

ii) Method is declared inside the class because it is an inline function and we need to parse it from LHS. Member functions are always evaluated from Left hand side.

iii) As of now class is not doind anything useful as it contains error. But when we resolve the errors, it can be used for incrementing a short value, which actually provides a kind of abstraction.

iv) There are few errors in the code: -

1. in the operator overloading function, DataStorer temp(data) is declared before incrementing the operator, So everytime, it will always take the old value and return it, because incremented value is not getting updated in the object temp.

2. DataStorer temp(data) needs a parametrized constructor in the program.

Below is a running prototype of program.

#include<iostream>

using namespace std;

class DataStorer {

short data;

public:

DataStorer(int a) {

this->data = a;

}

DataStorer operator++() {

DataStorer temp(++data);

return temp;

}

DataStorer operator++(int) {

DataStorer temp(data++);

return temp;

}

void print() {

cout<<data;

}

};

int main(){

DataStorer s(21);

DataStorer s1(21);

DataStorer a = ++s;

a.print();

DataStorer b = s1++;

cout<<endl;

b.print();

}

Sample Run: -

22

21

16) Below are the answers in the order of blanks appearing in the sentence.

fstream,istream,ostream,cin,cout,input file,output file,mutable(pass by reference)

17.

i) False

ii) True

iii) False

iv) False.... because it is a function that can be used as an object eg. passing multiple arguments in a function accepting single arguments.

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