Define a class named Document that contains a member variable of type string nam
ID: 665325 • Letter: D
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 overloaded 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 assignment 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++"); .
i got this so far it needs two more class email and file.
#include <iostream>
#include<string>
using namespace std;
class Document
{
private:
string text, sender, recipient, title;
public:
Document(){
text = "";
sender = "";
recipient = "";
title = "";
}
Document(string sen, string rec, string tit) {
text = sen + "" + rec + "" + tit;
}
string getText() const {
return text;
}
string Email() const {
return text;
}
Document & operator=(const Document &);
};
Document & Document::operator=(const Document& c) {
// real = c.real;
// imag = c.imag;
return *this;
}
bool ContainsKeyword(const Document& docObject, string keyword)
{
if (docObject.getText().find(keyword) != string::npos)
return true;
return false;
}
int main()
{
using namespace std;
Document c1("KP", "K", "P");
Document c2("KK", "P*", "p-");
ContainsKeyword(c2, "c++");
cout << "c1= " << c1.getText() << "+" << c1.Email() << "i" << endl;
cout << "c2= " << c2.getText() << "+" << c2.Email() << "i" << endl;
c2 = c1;
cout << "assign c1 to c2:" << endl;
cout << "c2= " << c2.getText() << "+" << c2.Email() << "i" << endl;
Explanation / Answer
//Document.h
#ifndef DOCUMENT_H
#define DOCUMENT_H
class Document
{
public:
Document();
Document(string txt);
Document(const Document& object);
string getText() const;
void setText(string txt);
Document& operator=(const Document& rtSide);
public:
string text;
};
#endif
//Document.cpp
#include "stdafx.h"
#include "Document.h"
#include<iostream>
#include<string>
using namespace std;
Document::Document():text("Not yet defined"){}
Document::Document(string txt):text(txt)
{
}
Document::Document(const Document& object)
{
text=object.text;
}
string Document::getText() const
{
return text;
}
void Document::setText(string txt)
{
text=txt;
}
Document& Document::operator=(const Document& rtSide)
{
text=rtSide.text;
return *this;
}
//Email.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
#include "Email.h"
Email::Email():Document(),sender("no sender"),recipient("No recipient"),title("No title"){}
Email::Email(string snd,string recip,string tit,string txt):Document(txt),sender(snd),recipient(recip),title(tit){}
Email::Email(const Email& emailObj):Document(emailObj)
{
sender=emailObj.sender;
recipient=emailObj.recipient;
title=emailObj.title;
}
string Email::getSender() const
{
return sender;
}
string Email::getRecipient() const
{
return recipient;
}
string Email::getTitle() const
{
return title;
}
void Email::setSender(string snd)
{
sender=snd;
}
void Email::setRecipient(string recip)
{
recipient=recip;
}
void Email::setTitle(string tit)
{
title=tit;
}
Email& Email::operator=(const Email& rtSide)
{
Document::operator=(rtSide);
sender=rtSide.sender;
recipient=rtSide.recipient;
title=rtSide.title;
return *this;
}
//File.h
#ifndef FILE_H
#define FILE_H
#include "Document.h"
class File: public Document
{
public:
File();
File(string pName,string txt);
File(const File& fileObj);
string getPathName() const;
void setPathName(string pName);
File& operator=(const File& rtSide);
private:
string pathName;
};
#endif
//File.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
#include "file.h"
File::File():Document(),pathName("No path name"){}
File::File(string pName,string txt): Document(txt), pathName(pName){}
File::File(const File& fileObj):Document(fileObj)
{
pathName=fileObj.pathName;
}
string File::getPathName() const
{
return pathName;
}
void File::setPathName(string pName)
{
pathName=pName;
}
File& File::operator=(const File& rtSide)
{
Document::operator=(rtSide);
pathName=rtSide.pathName;
return *this;
}
//main.cpp
#include "stdafx.h"//Use Viscual C++ IDE
#include "Email.h"
#include "File.h"
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
bool ContainsKeyword(const Document& docObj,string Keyword);
int main()
{
string snd,recip,tit,txt,pName,keyword;
Email email1("Martin","Sam","message","hello how are you");
cout<<"Email#1"<<endl;
cout<<"Sender's Name: "<<email1.getSender()<<endl
<<"Recipient's Name: "<<email1.getRecipient() <<endl<<"Title: "<<email1.getTitle()<<endl
<<"Message: "<<email1.getText()<<endl;
cout<<endl<<endl;
if(ContainsKeyword(email1,"fine"))
cout<<"Keyword present"<<endl;
else
cout<<"Keyword not present"<<endl;
Email email2;
cout<<"Enter the sender name: ";
getline(cin,snd);
email2.setSender(snd);
cout<<"Enter the recipient name: ";
getline(cin,recip);
email2.setRecipient(recip);
cout<<"Enter the title name: ";
getline(cin,tit);
email2.setTitle(tit);
cout<<"Enter the text: ";
getline(cin,txt);
email2.setText(txt);
cout<<endl;
cout<<"Email#2"<<endl;
cout<<"Sender's Name: "<<email2.getSender()<<endl
<<"Recipient'sName:"<<email2.getRecipient()<<endl
<<"Title: "<<email2.getTitle()<<endl
<<"Message: "<<email2.getText()<<endl;
cout<<endl<<endl;
cout<<"Enter the keyword to be search in Email#2:";
getline(cin,keyword);
if(ContainsKeyword(email2,keyword))
cout<<"Keyword present"<<endl;
else
cout<<"Keyword not present"<<endl;
File file1("data.txt","Good morning, welcome to visualstudio");
cout<<"File#1"<<endl;
cout<<"PathName: "<<file1.getPathName()<<endl
<<"Messge: "<<file1.getText()<<endl;
cout<<endl<<endl;
if(ContainsKeyword(file1,"visual"))
cout<<"KeyWord present"<<endl;
else
cout<<"Keyword not present"<<endl;
File file2;
cout<<"Enter the pathname: ";
getline(cin,pName);
file2.setPathName(pName);
cout<<"Enter the text: ";
getline(cin,txt);
file2.setText(txt);
cout<<"File#2"<<endl;
cout<<"2.PathName: "<<file2.getPathName()<<endl
<<"Messge: "<<file2.getText()<<endl;
cout<<endl<<endl;
cout<<"Enter the keyword to be serach in File#2:";
getline(cin,keyword);
if(ContainsKeyword(file2,keyword))
cout<<"Keyword present"<<endl;
else
cout<<"Keyword not present"<<endl;
return 0;
}
bool ContainsKeyword(const Document& docObj,string Keyword)
{
if(docObj.getText().find(Keyword)!=string::npos)
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.