Question: 6. The following C program is similar to the program that was shown on Tuesday (25th of Jan) in class: #include int main( int argc,

 6. The following C program is similar to the program thatwas shown on Tuesday (25th of Jan) in class: #include int main(int argc, char* argv[] ) { unsigned int linenr 1; = >

6. The following C program is similar to the program that was shown on Tuesday (25th of Jan) in class: #include int main( int argc, char* argv[] ) { unsigned int linenr 1; = > FILE* f = fopen( "myfile.in", "r" ); if(!f) { printf( "could not open the file " ); return -1; } int c = getc(f); while( c != EOF ) { if( c == $' ) { printf( "illegal dollar sign in line %u ", linenr ); return -1; } if (c == ' ') ++ linenr; C = getc(f); } fclose(f); return 0; } Your task is to make a C++ program from this C program, in the same way as was shown in class: (a) Create a struct openfile with one field FILE* f. (b) Add a destructor to openfile that closes the file f. (c) Create a constructor of openfile that tries to open a file for reading, so that you can write openfile of "myfile.in" ); in your main function. (d) Replace all occurrences of printf by . In order to do this, you have to #include (e) We want to make the field f inaccessible from outside, as was done in class. In order to do this, create the method int getchar( ) inside the definition of openfile (f) We cannot yet hide field f, because of the test !f that checks if file f is open. Create a method bool isopen( ) const inside the definition of openfile that returns true if f is open. Note: C++ has a boolean type. Use it! (g) Now you can make the field f private. The purpose of this exercise is that you learn two of the most important features of C++: The use of RAII (Resource Acquisition is Initialization) and encapsulation. (hiding implementation) Don't forget to submit your C++ program before the deadline

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!