Question

Provide the definition of class Message, which models an email message, in a file named Message.hpp...

Provide the definition of class Message, which models an email message, in a file named Message.hpp and implement all the required member functions (see below) in a file named Message.cpp.

A message contains a header and a body, which are separated by the first full line break in the message. The message header has a sender, a recipient, a subject representing the message heading, a Unix timestamp representing the sending time of the message, a character encoding, which you can safely assume is UTF-8, and a message identification number. The message body is the message text. The Message class supports the following member functions:

(a) A constructor that takes the sender and recipient and sets the timestamp to the current time.

(b) A member function append that takes a string as an argument, appends the string to message body, and returns nothing.

(c) A member function toString that takes no arguments, makes the message into one long string (see below for an example) and returns it.

From: Minerva McGonagall\nTo:Mr. H. Potter\n\nDear Harry...

(d) A member function print that takes no arguments and prints the message text.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Message.hpp

#ifndef MESSAGE_HPP_

#define MESSAGE_HPP_

#include <iostream>

#include <ctime>

using namespace std;

class Message

{

       string sender;

       string recipient;

       string subject ;

       time_t timestamp;

       string character_encoding;

       long int identification_number;

       string text;

       static long int count;

public:

       Message(string sender, string recipient);

       void append(string txt);

       string toString();

       void print();

       void setSubject(string subject);

};

#endif /* MESSAGE_HPP_ */

//end of Message.hpp

// Message.cpp

#include "Message.hpp"

#include <string>

long int Message::count = 0;

// constructor to set the sender, recipient and timestamp

Message::Message(string sender, string recipient)

{

       this->sender = sender;

       this->recipient = recipient;

       timestamp = time(NULL); // returns unix epoch time

       character_encoding = "UTF-8";

       count++;

       identification_number = count;

       subject = "";

       text = "";

}

// function to append message to message text

void Message:: append(string txt)

{

       text += txt;

}

// function to set the subject of the message

void Message::setSubject(string subject)

{

       this->subject = subject;

}

// function to return the string representation of the message

string Message:: toString()

{

       string messageStr = "From : "+sender+"\n";

       messageStr += "To : ";

       messageStr += recipient.substr(0,1)+". "+recipient.substr(recipient.find(' ')+1);

       messageStr += "\n\n";

       messageStr += "Dear "+recipient.substr(0,recipient.find(' '))+",\n";

       messageStr += text;

       return messageStr;

}

// function to print the message text

void Message::print()

{

       cout<<text;

}

//end of Message.cpp

// main.cpp : C++ program to test the message class

#include "Message.hpp"

#include <iostream>

using namespace std;

int main()

{

       Message m("Minerva McGonagall","Harry Potter");

       m.setSubject("Hi");

       m.append("How are you?\nI am good");

       cout<<m.toString();

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Provide the definition of class Message, which models an email message, in a file named Message.hpp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the Message class using a header and implementation file named Message.h and Message.cpp respectively. In...

    Implement the Message class using a header and implementation file named Message.h and Message.cpp respectively. In addition to the two Message class files, you must write a driver. The driver must create a Message array that is capable of holding 100 messages (make sure that you don’t exceed the maximum number of Messages). It then must read all Messages from a file named messages.txt (ordered date\n sender\n recipient\n body\n), sort the Messages based on date, find a Message based on...

  • C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names...

    C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient. The header file for this class is given below. #ifndef LETTER_H #define LETTER_H #include #include using namespace std; class Letter { public:    //constructor    Letter(string from = "John", string to = "Ana");    void add_line(string line);    string get_text() const; private:    string sender;    string recipient;    vector lines; }; #endif -Implement the constructor to initialize...

  • c++ Program Step 1: Design a class Message that models an e-mail message. A message has...

    c++ Program Step 1: Design a class Message that models an e-mail message. A message has a recipient, a sender, and a message text. Support the following member functions:

  • Write a definition for a class named Book with attributes title, price and author, where author...

    Write a definition for a class named Book with attributes title, price and author, where author is a Contact object and title is a String, and price is a float number. You also need to define the Contact class, which has attributes name and email, where name and email are both String. Instantiate a couple of Book objects that represents books with title, price and author (You can come up with the attributes values for each object) Write a function...

  • Create a class named Game using the following UML class diagram. GAME -gameName : string +Game()...

    Create a class named Game using the following UML class diagram. GAME -gameName : string +Game() +setGameName(in name : string) : void +getGameName() : string +displayMessage() : void This class has 2 member variables namely playerName and playerScore. The class contain following member functions: Constructor, set function, get functions, display function. Code write in 3 files: Game.h header file, funtion.cpp file and main.cpp file. The output should be: Player John has scored 50 points.

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • How do I format this into C++? This the prompt: Design a class named Password that...

    How do I format this into C++? This the prompt: Design a class named Password that stores a password in a c-string and has member functions to test if the password complies with certain requirements as follows: The password should be between 6 and 20 characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. The password should have at least one punctuation character. Define a...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT