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

//text writer //Include libraries #include<iostream> #include<string> #include<f

ID: 3866523 • Letter: #

Question

//text writer

//Include libraries

#include<iostream>

#include<string>

#include<fstream>

//Use namespace

using namespace std;

//Define a class

class TextWriter

{

     //Define access specifier

     private:

     //Declare variable

     string *filename;

     //Declare variable

     ofstream fstream, *fstream_ptr;

     //Declare variable

     string default_str ;

     //Define access specifier

     public:

     //Define a constructor

     TextWriter()

     {

          //Define file name

          default_str = "sample.txt";

          //Assign

          filename = &default_str;

          //Open

          fstream.open(default_str);

          //Assign pointer

          fstream_ptr= &fstream;

     }

     //Define a constructor

     TextWriter(string *filename1)

     {

          //Set filename

          filename = filename1;

     }

     //Define a constructor

     TextWriter(TextWriter& wrt)

     {

          //Declare string

          string tp = wrt.get_filename();

          //Set file name

          set_filename(&tp);

     }

     //Define a method

     void set_filename(string *filename1)

     {

          //Set file name

          filename = filename1;

     }

     //Define a method

     string& get_filename()

     {

          //Return

          return *filename ;

     }

     //Define a method

     void writeFile()

     {

          //Declare variable

          ofstream fid;

          //Exception

          fid.exceptions(ofstream::failbit|ofstream::badbit);

          //Define try block

          try

          {

              //Open

              fid.open(*filename);

              //Display message

              fid<<"Hello world"<<endl;

              //Close

              fid.close();

          }

          //Define catch

          catch(ofstream::failure e)

          {

              //Display message

              std::cerr<<"Error in writing "<<endl;

          }

     }

};

//Define main

int main(int nInputs, char*inputs[])

{

     //Declare variable

     TextWriter *w = new TextWriter;

     //Write

     cout<<(w->get_filename())<<endl;

     //Call method

     w->writeFile();

     //Pause console window

     system("pause");

     //Return

     return 0;

}

Part 2: Inheritance (concepts from Chpt. 15). 1) Derive an SVGWriter class from TextWriter (Chpt. 15) so that it has the following functions: string get_header(int w, int h, Colour c) /* this function generates header string to be inserted into an SVG file* { string text = "in" " '" ""; // write code below so that input arguments will be used to replace WIDTH and HEIGHT return text; SVGWriter::"SVGWriter(); Write(In "); // redefined function that writes input argument string to stream cout

Explanation / Answer

#include<iostream>

#include<string>

#include<fstream>

//Use namespace

using namespace std;

//Define a class

class TextWriter

{

     //Define access specifier

     private:

     //Declare variable

     string *filename;

     //Declare variable

     ofstream fstream, *fstream_ptr;

     //Declare variable

     string default_str ;

     //Define access specifier

     public:

     //Define a constructor

     TextWriter()

     {

          //Define file name

          default_str = "sample.txt";

          //Assign

          filename = &default_str;

          //Open

          fstream.open(default_str);

          //Assign pointer

          fstream_ptr= &fstream;

     }

     //Define a constructor

     TextWriter(string *filename1)

     {

          //Set filename

          filename = filename1;

     }

     //Define a constructor

     TextWriter(TextWriter& wrt)

     {

          //Declare string

          string tp = wrt.get_filename();

          //Set file name

          set_filename(&tp);

     }

     //Define a method

     void set_filename(string *filename1)

     {

          //Set file name

          filename = filename1;

     }

     //Define a method

     string& get_filename()

     {

          //Return

          return *filename ;

     }

     //Define a method

     void writeFile()

     {

          //Declare variable

          ofstream fid;

          //Exception

          fid.exceptions(ofstream::failbit|ofstream::badbit);

          //Define try block

          try

          {

              //Open

              fid.open(*filename);

              //Display message

              fid<<"Hello world"<<endl;

              //Close

              fid.close();

          }

          //Define catch

          catch(ofstream::failure e)

          {

              //Display message

              std::cerr<<"Error in writing "<<endl;

          }

     }

};

//Define main

int main(int nInputs, char*inputs[])

{

     //Declare variable

     TextWriter *w = new TextWriter;

     //Write

     cout<<(w->get_filename())<<endl;

     //Call method

     w->writeFile();

     //Pause console window

     system("pause");

     //Return

     return 0;

}