juinda

Document

Jun 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. //--------------
  2. //----file.h----
  3. //--------------
  4.  
  5. #ifndef FILE_H
  6. #define FILE_H
  7.  
  8. #include "document.h"
  9.  
  10. // Define class for File, derive from Document
  11. // For brevity, short one-line methods are defined here in the
  12. // header.
  13.  
  14. class File : public Document
  15. {
  16. public:
  17. File() : Document()
  18. {
  19. }
  20. File(string body, string pathname) :
  21. Document(body), pathname(pathname)
  22. {
  23. }
  24. void setPathname(string s)
  25. {
  26. pathname = s;
  27. }
  28. string getPathname()
  29. {
  30. return pathname;
  31. }
  32. File& operator =(const File& rightSide);
  33. private:
  34. string pathname;
  35. };
  36.  
  37. #endif // !FILE_H
  38.  
  39. //----------------
  40. //----file.cpp----
  41. //----------------
  42.  
  43. #include "file.h"
  44.  
  45. // ======================
  46. // Assignment operator
  47. // Invoke the base operator and then
  48. // copy all variables from the right to left.
  49. // ======================
  50. File& File::operator =(const File& rightSide)
  51. {
  52. // First invoke base operator
  53. Document::operator =(rightSide);
  54. // Simply copy the other string values
  55. pathname = rightSide.pathname;
  56. return *this;
  57. }
  58.  
  59. //---------------
  60. //----email.h----
  61. //---------------
  62.  
  63. #ifndef EMAIL_H
  64. #define EMAIL_H
  65.  
  66. #include "document.h"
  67.  
  68. // Define class for Email, derive from Document
  69. // For brevity, short one-line methods are defined here in the
  70. // header.
  71.  
  72. class Email : public Document
  73. {
  74. public:
  75. Email() : Document()
  76. {
  77. }
  78. Email(string body, string sender, string recipient, string title) :
  79. Document(body), sender(sender), recipient(recipient),
  80. title(title)
  81. {
  82. }
  83. void setRecipient(string s)
  84. {
  85. recipient = s;
  86. }
  87. string getRecipient()
  88. {
  89. return recipient;
  90. }
  91. void setSender(string s)
  92. {
  93. sender = s;
  94. }
  95. string getSender()
  96. {
  97. return sender;
  98. }
  99. void setTitle(string s)
  100. {
  101. title = s;
  102. }
  103. string getTitle()
  104. {
  105. return title;
  106. }
  107. Email& operator =(const Email& rightSide);
  108. private:
  109. string sender, recipient, title;
  110. };
  111.  
  112. #endif // !EMAIL_H
  113.  
  114. //-----------------
  115. //----email.cpp----
  116. //-----------------
  117.  
  118. #include "email.h"
  119.  
  120. // ======================
  121. // Assignment operator
  122. // Invoke the base operator and then
  123. // copy all variables from the right to left.
  124. // ======================
  125. Email& Email::operator =(const Email& rightSide)
  126. {
  127. // First invoke base operator
  128. Document::operator =(rightSide);
  129. // Simply copy the other string values
  130. sender = rightSide.sender;
  131. recipient = rightSide.recipient;
  132. title = rightSide.title;
  133. return *this;
  134. }
  135.  
  136.  
  137. //------------------
  138. //----document.h----
  139. //------------------
  140.  
  141. #ifndef DOCUMENT_H
  142. #define DOCUMENT_H
  143.  
  144. //document.cpp
  145. //
  146. // This program introduces inheritance through a problem of
  147. // creating two types of Documents, email and files.
  148. // Both inherit from Document with a "text" property that
  149. // we use to search for keywords of any subclass.
  150.  
  151. #include <iostream>
  152. #include <string>
  153.  
  154. using namespace std;
  155.  
  156.  
  157. // Base class for a Document.
  158. // For brevity, short one-line methods are defined here in the
  159. // header.
  160.  
  161. class Document
  162. {
  163. public:
  164. Document()
  165. {
  166. text = "";
  167. }
  168. Document(string text)
  169. {
  170. this->text = text;
  171. }
  172. string getText() const
  173. {
  174. return this->text;
  175. }
  176. Document& operator =(const Document& rightSide);
  177. private:
  178. string text;
  179. };
  180.  
  181. bool ContainsKeyword(const Document& docObject, string keyword);
  182.  
  183. #endif // !DOCUMENT_H
  184.  
  185. //--------------------
  186. //----document.cpp----
  187. //--------------------
  188.  
  189. #include "document.h"
  190.  
  191. // ======================
  192. // Assignment operator
  193. // simply copy the string for the text.
  194. // ======================
  195. Document& Document::operator =(const Document& rightSide)
  196. {
  197. // Simply copy the string for the text value
  198. this->text = rightSide.text;
  199. return *this;
  200. }
  201.  
  202. // ======================
  203. // ContainsKeyword
  204. // Returns true if the Document
  205. // object passed in contains keyword as a substring
  206. // of its text property.
  207. // ======================
  208. bool ContainsKeyword(const Document& docObject, string keyword)
  209. {
  210. if (docObject.getText().find(keyword) != string::npos) return true;
  211. return false;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment