Define a class named Document that contains an instance variable of type string
ID: 3840830 • Letter: D
Question
Define a class named Document that contains an instance variable of type string named text that stores any textual content for the document. Include methods to set and get this text value. Create method named printDocument that prints the text field. Include appropriate constructor(s).
Next, define a class for email that is derived from Document and includes instance variables for the sender, recipient, and title for an email message. Implement appropriate accessor and mutator methods. The body of the email message should be stored in the inherited variable text. Define the printEmail method to print the text and other appropiate fields. Include appropriate constructor(s). Similarly, define a class for File that is derived from Document and includes an instance variable for the pathname. The textual contents of the file should be stored in the inherited variable text. Define the printFile method to concatenate and print all text appropriate fields. Include appropriate constructor(s). Finally, create several sample objects of type Email and File in your main function. (Please, the answer should be in C++ language).
Explanation / Answer
#include <iostream>
using namespace std;
class Document
{
private:
string text;
public:
Document()
{
text="";
}
Document(string t)
{
text=t;
}
string getText()
{
return text;
}
void setText(string t)
{
text=t;
}
void operator=(Document d)
{
text=d.text;
}
};
class Email:public Document
{
private:
string sender;
string recipient;
string title;
public:
Email():Document()
{
sender="";
recipient="";
title="";
}
Email(string tt,string s,string r,string t):Document(tt)
{
sender=s;
recipient=r;
title=t;
}
void setSender(string s)
{
sender=s;
}
void setTitle(string t)
{
title=t;
}
void setRecipient(string r)
{
recipient=r;
}
string getTitle()
{
return title;
}
string getSender()
{
return sender;
}
string getRecipient()
{
return recipient;
}
void operator=(Email e)
{
Document:setText(e.getText());
sender=e.sender;
recipient=e.recipient;
title=e.title;
}
};
class File:public Document
{
private:
string pathname;
public:
File():Document()
{
pathname="";
}
File(string t,string p):Document(t)
{
pathname=p;
}
string getPathname()
{
return pathname;
}
void setPathName(string p)
{
pathname=p;
}
void operator=(File f)
{
Document:setText(f.getText());
pathname=f.pathname;
}
};
bool ContainsKeyword(Document& docObject, string keyword)
{
if (docObject.getText().find(keyword) != string::npos)
return true;
return false;
}
int main()
{
Email e1("Which exm do we have today?","X","Y","Exm??"),e2("We have C++","Y","X","Exm??");
File f1("C++ Notes","C:/Doc/C++"),f2("C Notes","C;/Doc/C");
if(ContainsKeyword(e1,"C++"))
cout<<"e1 contains c++"<<endl;
else
cout<<"e1 doesn't contain c++"<<endl;
if(ContainsKeyword(e2,"C++"))
cout<<"e2 contains c++"<<endl;
else
cout<<"e2 doesn't contain c++"<<endl;
if(ContainsKeyword(f1,"C++"))
cout<<"f1 contains c++"<<endl;
else
cout<<"f1 doesn't contain c++"<<endl;
if(ContainsKeyword(f2,"C++"))
cout<<"f2 contains c++"<<endl;
else
cout<<"f2 doesn't contain c++"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.