Description
The purpose of this challenge is to implement basic class functionality (setters, getters, constructor, destructor). This challenge simulates part of a messaging application.
Requirements
- Create a class called Message. For the entirety of the assignment, be sure to NOT use cout or cin inside the class.
- Add the following private data members
int id; string sender; string text; bool read;
- Add a private function, clear_message(). Use this function to clear all the private data members (blank the strings, set bool member(s) to false)
- Add a default constructor that will call clear_message(). Then, set sender to “Unknown”
- Add an overloaded constructor that will pass three parameters. These 3 parameters will be used to set the values of id, sender and text. Set read to false.
- Add a destructor to append to a file called “message_log.txt”. In this file, append a line that looks like this “Destroyed message {id} from {sender}” The placeholders {id} and {sender} will be replaced by the actual values for the class. Then, call clear_message().
- Add a setter function to set the text data member.
- Add a setter function to set the sender data member.
- Add a setter function to set the id data member. Only set the id member as long as it is greater than zero
- Add a setter function called markRead(). This sets read to true.
- Add 3 getter functions, one for each of the data members id, sender and read.
- Add the following getter function
string summary() const { return "ID: " + to_string(id) + " | Sender: " + sender + " | Text: " + text + " | Status: " + (read ? "Read" : "Unread"); } - Use the sample main() as given.
Sample main()
int main()
{
// start the log file from scratch
ofstream clearLog("message_log.txt");
clearLog << "Message destruction log\n";
clearLog.close();
const int SIZE = 3;
// inbox is an array of Message objects
// each element of the array is created as an object
// calling the Message overloaded constructor
Message inbox[SIZE] = {
Message(101, "Ava", "Are you coming?"),
Message(102, "Leo", "Meeting at 2."),
Message(103, "Mia", "Send the notes.")
};
cout << "A destructor log entry was written for the preview message." << endl;
int choice, id;
string newText;
do
{
cout << "\n1. Display All\n2. Mark Read\n3. Edit Text\n4. Quit\nChoice: ";
cin >> choice;
if (choice == 1)
{
for (int i = 0; i < SIZE; i++)
{
cout << inbox[i].summary() << endl;
}
}
else if (choice == 2)
{
cout << "Enter message ID: ";
cin >> id;
for (int i = 0; i < SIZE; i++)
{
if (inbox[i].getId() == id)
{
inbox[i].markRead();
}
}
}
else if (choice == 3)
{
cout << "Enter message ID: ";
cin >> id;
cin.ignore();
cout << "Enter new text: ";
getline(cin, newText);
for (int i = 0; i < SIZE; i++)
{
if (inbox[i].getId() == id)
{
inbox[i].setText(newText);
}
}
}
} while (choice != 4);
cout << "Check message_log.txt to see activity." << endl;
return 0;
}
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL00081
Print Requirements
