Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------
- //----file.h----
- //--------------
- #ifndef FILE_H
- #define FILE_H
- #include "document.h"
- // Define class for File, derive from Document
- // For brevity, short one-line methods are defined here in the
- // header.
- class File : public Document
- {
- public:
- File() : Document()
- {
- }
- File(string body, string pathname) :
- Document(body), pathname(pathname)
- {
- }
- void setPathname(string s)
- {
- pathname = s;
- }
- string getPathname()
- {
- return pathname;
- }
- File& operator =(const File& rightSide);
- private:
- string pathname;
- };
- #endif // !FILE_H
- //----------------
- //----file.cpp----
- //----------------
- #include "file.h"
- // ======================
- // Assignment operator
- // Invoke the base operator and then
- // copy all variables from the right to left.
- // ======================
- File& File::operator =(const File& rightSide)
- {
- // First invoke base operator
- Document::operator =(rightSide);
- // Simply copy the other string values
- pathname = rightSide.pathname;
- return *this;
- }
- //---------------
- //----email.h----
- //---------------
- #ifndef EMAIL_H
- #define EMAIL_H
- #include "document.h"
- // Define class for Email, derive from Document
- // For brevity, short one-line methods are defined here in the
- // header.
- class Email : public Document
- {
- public:
- Email() : Document()
- {
- }
- Email(string body, string sender, string recipient, string title) :
- Document(body), sender(sender), recipient(recipient),
- title(title)
- {
- }
- void setRecipient(string s)
- {
- recipient = s;
- }
- string getRecipient()
- {
- return recipient;
- }
- void setSender(string s)
- {
- sender = s;
- }
- string getSender()
- {
- return sender;
- }
- void setTitle(string s)
- {
- title = s;
- }
- string getTitle()
- {
- return title;
- }
- Email& operator =(const Email& rightSide);
- private:
- string sender, recipient, title;
- };
- #endif // !EMAIL_H
- //-----------------
- //----email.cpp----
- //-----------------
- #include "email.h"
- // ======================
- // Assignment operator
- // Invoke the base operator and then
- // copy all variables from the right to left.
- // ======================
- Email& Email::operator =(const Email& rightSide)
- {
- // First invoke base operator
- Document::operator =(rightSide);
- // Simply copy the other string values
- sender = rightSide.sender;
- recipient = rightSide.recipient;
- title = rightSide.title;
- return *this;
- }
- //------------------
- //----document.h----
- //------------------
- #ifndef DOCUMENT_H
- #define DOCUMENT_H
- //document.cpp
- //
- // This program introduces inheritance through a problem of
- // creating two types of Documents, email and files.
- // Both inherit from Document with a "text" property that
- // we use to search for keywords of any subclass.
- #include <iostream>
- #include <string>
- using namespace std;
- // Base class for a Document.
- // For brevity, short one-line methods are defined here in the
- // header.
- class Document
- {
- public:
- Document()
- {
- text = "";
- }
- Document(string text)
- {
- this->text = text;
- }
- string getText() const
- {
- return this->text;
- }
- Document& operator =(const Document& rightSide);
- private:
- string text;
- };
- bool ContainsKeyword(const Document& docObject, string keyword);
- #endif // !DOCUMENT_H
- //--------------------
- //----document.cpp----
- //--------------------
- #include "document.h"
- // ======================
- // Assignment operator
- // simply copy the string for the text.
- // ======================
- Document& Document::operator =(const Document& rightSide)
- {
- // Simply copy the string for the text value
- this->text = rightSide.text;
- return *this;
- }
- // ======================
- // ContainsKeyword
- // Returns true if the Document
- // object passed in contains keyword as a substring
- // of its text property.
- // ======================
- bool ContainsKeyword(const Document& docObject, string keyword)
- {
- if (docObject.getText().find(keyword) != string::npos) return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment