Sunday, July 12, 2009

Help with an C++ homework 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++ homework question.?
The first problem with this is that you say you want to implement the function:





void remove_message(int i) const





This doesn't make sense, since only member functions can be declared const. Global functions can never be declared const. I assume you meant:





void Mailbox::remove_message(int i) const





The problem with this is that if this function is meant to effectively delete some data from the Mailbox class, then it is not legally allowed to do so because the function is declared as being const, which means it is not allowed to alter any members of the class of which it is itself a member.





However, the following would be legal:





void Mailbox::remove_message(int i)





In order to implement this in your version of the Mailbox class you would do:





void Mailbox::remove_message(int i)


{


messages.erase( members.begin() + i );





// Your compiler might not like the members.begin() + i.


// If this is the case use the following code instead





// std::vector%26lt; Message %26gt;::iterator itr;


// for ( int iCount = 0; iCount %26lt; i; iCount++ )


// {


// ++itr;


// }


// messages.erase( itr );


}





While your approach would work, it's not what you've been asked to do. The question states:





"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 means you shouldn't be using a member vector to store the messages. Instead you should be using a single very long string member to store all the messages, which you then parse to find a particular message. Basically, all the code you have to write is a bunch of string manipulation functions. I suggest you use a STL string object rather than a C style char array as this means you don't have to have a maximum string length for your storage string when you declare it nor do you have to manage the string's internal memory allocation yourself.





As you haven't included any details about the Message class I can't tell how you extract a string from a Message. Is Message a class/struct that has a string member, or is it just a typedef to std::string?


No comments:

Post a Comment