Sunday, July 12, 2009

Help with an C++ assignment question.?

i'm having problems creating and implementing the final requirment:void remove_message(int i) const;





This is my question:





You have to create a class called mailbox. You don't yet know how to store a collection of message objects. Instead, use the following brute force approach: The mailbox contains one very long string, which is the concatenation of all messages. You can tell where a new message starts by searching for a From: at the beginning of a line. This may sound like a dumb strategy, but surprisingly, many e-mail systems do just that.


Implement the following member functions:


void Mailbox::add_message(Message m);


Message Mailbox::get_message(int i) const;


void remove_message(int i) const;








I would appreciate any input or comments anyone can make about what I have so far and if what I am doing or trying to do makes sense.








class Mailbox


{


public:


Mailbox(string u);


void add_message(Message m);


Message get_message(int i) const;


int count_messages() const;


string get_user() const;


private:


vector%26lt;Message%26gt; messages;


string user;


};





Mailbox::Mailbox(string u)


{


user = u;


}





string Mailbox::get_user() const


{


return user;


}





void Mailbox::add_message(Message m)


{


messages.push_back(m);


}





Message Mailbox::get_message(int message_num) const


{


return messages[message_num];


}





int Mailbox::count_messages() const


{


return messages.size();


}

Help with an C++ assignment question.?
You don't show how Message is defined, but with vector%26lt;Message%26gt;, which would probably give you a nicer implementation, I don't think you're doing what the assignment asks. Mailbox::messages needs to be a string; and for convenience, give Mailbox a count attribute.





The signature of add_message would be :


void Mailbox::add_message(const string%26amp; m);


It would use string's operator+= to concatenate m to messages, and then increment the count attribute. (Mailbox::remove_message decrements the count attribute, and count_messages just returns the current value of count.)





The string class provides various find operations you can use to search for a message, using the keyword "From:", that get_message or remove_message is looking for.





Assignments like this usually make you do something one way when you know there's an easier way to do it. So, use a string instead of your vector, and you'll probably learn a few things about all the good operations the string class provides.

survey questions

No comments:

Post a Comment