SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:7 | ISBN:9780132846813 | Edition: 5

Question

Define a class named Document that contains a member variable of type string named text that stores any textual content for the document. Create a function named getText that returns the text field, a way to set this value, and an over- loaded assignment operator.

Next, define a class for Email that is derived from Document and that includes member variables for the sender , recipient , and title of an e-mail message. Implement appropriate accessor and mutator functions. The body of the e-mail message should be stored in the inherited variable text . Also overload the assign- ment operator for this class.

Similarly, define a class for File that is derived from Document and that includes a member variable for the pathname. Implement appropriate accessor and mutator functions for the pathname and overload the assignment operator. Finally, create several sample objects of type Email and File in your main function. Test your objects by passing them to the following subroutine, which will return true if the object contains the specified keyword in the text property.

bool ContainsKeyword( const Document& docObject, string keyword)

{

if (docObject.getText().find(keyword) != string::npos)

return true;

return false;

}

For example, you might test to see whether an e-mail message contains the text "c++" with the call ContainsKeyword(emailObj, "c++"); .


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGAM CODE:

//Header section
#include <iostream>
#include <string>


class Document 
{
protected:
    string text;

public:
    // Constructor
    Document() {}

    // Accessor function to get the text
    string getText() const {
        return text;
    }

    // Mutator function to set the text
    void setText(const string& new_text) {
        text = new_text;
    }

    // Overloaded assignment operator
    Document& operator=(const Document& other) {
        if (this != &other) {
            text = other.text;
        }
        return *this;
    }
};

class Email : public Document {
private:
    string sender;
    string recipient;
    string title;

public:
    // Constructor
    Email(const string& sender, const string& recipient, const string& title, const string& body) {
        this->sender = sender;
        this->recipient = recipient;
        this->title = title;
        this->text = body;
    }

    // Accessor functions for sender, recipient, and title
    string getSender() const {
        return sender;
    }

    string getRecipient() const {
        return recipient;
    }

    string getTitle() const {
        return title;
    }

    // Overloaded assignment operator for Email
    Email& operator=(const Email& other) {
        if (this != &other) {
            sender = other.sender;
            recipient = other.recipient;
            title = other.title;
            text = other.text;
        }
        return *this;
    }
};

class File : public Document {
private:
    string pathname;

public:
    // Constructor
    File(const string& pathname, const string& content) {
        this->pathname = pathname;
        this->text = content;
    }

    // Accessor function for pathname
    string getPathname() const {
        return pathname;
    }

    // Mutator function for pathname
    void setPathname(const string& new_pathname) {
        pathname = new_pathname;
    }

    // Overloaded assignment operator for File
    File& operator=(const File& other) {
        if (this != &other) {
            pathname = other.pathname;
            text = other.text;
        }
        return *this;
    }
};

// Function to check if the given keyword is present in the text of the Document
bool ContainsKeyword(const Document& docObject, const string& keyword) {
    return docObject.getText().find(keyword) != string::npos;
}

int main() {
    // Creating sample objects of type Email and File
    Email emailObj("sender@example.com", "recipient@example.com", "Important message", "Hello, this is a test email containing some C++ code.");
    File fileObj("path/to/file.txt", "This is some sample text in a file containing C++ code.");

    // Testing ContainsKeyword function with Email and File objects
    if (ContainsKeyword(emailObj, "C++")) {
        cout << "The email contains the keyword 'C++'." << endl;
    } else {
        cout << "The email does NOT contain the keyword 'C++'." << endl;
    }

    if (ContainsKeyword(fileObj, "C++")) {
        cout << "The file contains the keyword 'C++'." << endl;
    } else {
        cout << "The file does NOT contain the keyword 'C++'." << endl;
    }

    return 0;
}

OUTPUT:

The email contains the keyword 'C++'.
The file contains the keyword 'C++'.

 

0 0

Discussions

Post the discussion to improve the above solution.