Fill This Form To Receive Instant Help
Homework answers / question archive / Workshop #9: Derived Classes and Resources Version 1
Workshop #9: Derived Classes and Resources
Version 1.1 - overview session added
In this workshop, you are to code/complete two classes:
If we have any additional custom code, (i.e. functions, classes etc) that we want to reuse in the workshop save them under a module called Utils (Utils.cpp and Utils.h) and submit them with workshop using the instructions in the "Submitting Utils Module" section.
Lab (Part 1, 100%)
Text class
Text class is created by a default constructor that sets the class to an empty state.
Character pointer attribute for content (private)
Text class has only one attribute (m_content) that holds the content of the text file dynamically.
Methods
const char& operator[](int index)const; (protected)
This index operator provides read-only access to the content of the text for the derived classes of Text.
The behaviour of the operator is not defined if the index goes out of bounds or if the content is null.
The rule of three
Implement the rule of three so the allocated memory is managed properly in case of copying and assignment.
Receives an istream reference and returns it at the end.
Receives an ostream reference and returns it at the end of the function.
This virtual function will insert the content of the Text class into the ostream if m_content is not null. Also, this function does not change the content of a file.
Helper functions
insertion and extraction overload into ostream and from istream
Overload the insertion and extraction operators using the write and read methods
Usage Sample
If the file **test.txt" has the following content:
abcndefg
Having the following code snippet:
Text T; Text Y; ifstream test("test.txt"); test >> T; Y = T; Text Z = Y; T = Z; cout << "*" << T << "*" << endl;
The output will be:
*abc
defg*
getFileLength
To be able to allocate dynamic memory and read the content of a file into the memory, we need to know the size of the text file in bytes.
The following function returns the number of bytes in an istream object or the value zero if the stream is in an invalid state.
unsigned getFileLength(istream& is) { unsigned len{}; if (is) { // save the current read position std::streampos cur = is.tellg(); // go to the end is.seekg(0, ios::end); // tell what is the positions (end position = size) len = unsigned(is.tellg()); // now go back to where you were. is.seekg(cur); } return len; }
We can either add this as a method to the Text class or add it as a utility method in your Utils class (if you are using the Utils class in the workshop.)
HtmlText class
HtmlText class is inherited from the Text for an HTML conversion. Make sure, like the Text class, the rule of three is implemented here.
HtmlText add a character pointer as an attribute for holing the title of the HTML file (m_title)
HtmlText class is created using an argument for the title of the HTML conversion and overrides the virtual write function of the base class.
HtmlText is created using title. The constructor will keep the title dynamically as a property. If the provided title is null, the HTML file will have no title, but it is still a valid HTML file.
char* m_title property
Holds the title of the HTML conversion dynamically.
Text::write override
This override will convert the text file to a simple HTML format when writing file as follows:
First the open tags for html page, header and title are inserted into ostream proceeding with the open body tag. Then for all the content of the file when writing:
In the end, the close tags for body and HTML tags are inserted.
We can use our own logic to implement the above, or use the following guidelines:
To accomplish the above do the insertion into ostream as follows:
Based on the value of the current element in the loop
In case the value is a space
if MS is true insert " "
otherwise set MS to true and then insert a space (' ')
In case the value is '<'
insert "<" and set the MS to false
In case the value is '>'
insert ">" and set the MS to false
In case the value is newline 'n'
insert "<br />n" and set the MS to false
In all other cases
set the MS to false and insert the current value, unchanged.
Tester program
Another version of this program with a larger data file, that is used for submission, is added to this directory at submission under the name main_prof.cpp
/* ------------------------------------------------------ Workshop 9 part 1 Module: N/A Filename: main.cpp Version 1.1 Author: Fardad Soleimanloo 22/03/26 Revision History ----------------------------------------------------------- Date Reason 22/03/27 Missed #include <fstream> -----------------------------------------------------------*/ #include <iostream> #include <fstream> #include "HtmlText.h" #include "Text.h" using namespace std; using namespace sdds; void show(Text T) { cout << "*" << T << "*" << endl; } void saveHtml(HtmlText H) { ofstream("dcwrLittle.html")<<H; } void displayFile(const char* filename) { cout << "File: " << filename << endl; cout << "==================================================" << endl; ifstream fin(filename); char ch = 0; while (fin.get(ch)) { cout << ch; } cout << endl << "==================================================" << endl; } int main() { Text T; Text Y; Text Z; ifstream test("test.txt"); ifstream dcwrLittle("dcwrLittle.txt"); test >> T; dcwrLittle >> Y; Y = T; Z = Y; show(Y); HtmlText H1("Derived Classes and Resources"); HtmlText H2; HtmlText H3; dcwrLittle.seekg(0); dcwrLittle >> H1; dcwrLittle.seekg(0); dcwrLittle >> H2; H2 = H1; H3 = H2; saveHtml(H3); displayFile("dcwrLittle.html"); return 0; }
output
*abc
defg*
File: dcwrLittle.html
==================================================
<html><head><title>Derived Classes and Resources</title></head>
<body>
<h1>Derived Classes and Resources</h1>
Design classes with dynamically allocated resources to model the components of a programming solution<br />
Define the copy constructor and assignment operator for a derived class with a resource<br />
Identify the copy constructor and copy assignment operator defaults for a derived class<br />
"If you use pointers, you have to think about resource management" (Stroustrup, 1997)<br />
<br />
Inheritance hierarchies that access resources at multiple levels require intervention. Managing relationships between the special member functions in a hierarchy with multiple resources involves ensuring that the appropriate calls between these functions are made. The definitions of some copy constructors and copy assignment operators in the hierarchy may require explicit coding of the connections to their base class counterparts. <br />
<br />
This chapter describes how to define the constructors and the copy assignment operators in a hierarchy that access multiple resources and how to call their base class counterparts. <br />
<br />
<br />
CONSTRUCTORS AND DESTRUCTOR<br />
<br />
Each constructor of a derived class calls a constructor of its base class. By default, that constructor is the no-argument constructor. To override this default, we insert an explicit call to the base class constructor. <br />
<br />
Destructors in an inheritance hierarchy do not require any intervention, since each class in the hierarchy has but one destructor and each destructor calls its sole base class counterpart automatically. <br />
<br />
Example<br />
<br />
Let us upgrade the definition of our Student class to accommodate a client-defined number of grades. We store the grades in dynamic memory and store the address of that memory in a resource instance pointer. <br />
<br />
The upgraded definition of our Student class contains a resource instance pointer:<br />
<br />
// Student.h<br />
<br />
#include <iostream><br />
const int NC = 30;<br />
<br />
class Person {<br />
char name[NC+1];<br />
public:<br />
Person();<br />
Person(const char*);<br />
void display(std::ostream&) const; <br />
};<br />
<br />
class Student : public Person {<br />
int no;<br />
float* grade;<br />
int ng;<br />
public:<br />
Student();<br />
Student(int);<br />
Student(const char*, int, const float*, int); <br />
~Student();<br />
void display(std::ostream&) const;<br />
};</body>
</html>
==================================================