Mailbox

Hello everyone, and I'm glad I found this forum. I am stuck in a bind and don't know where to go from here. I have a message class and a mailbox class. I need to be able to add messages into a virtual "mailbox" (mailbox.cpp), I have 75% of everything done, I just dont know how to implement a few of the remaining functions in the mailbox class, as seen below. I am a beginner at C++ and I'm sure if someone can help me get going, I can finish the rest. Thank you guys for the help, this is what I have written thus far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main
#include <iostream>
#include <string>
#include "message.h"
#include "mailbox.h"
using namespace std;

int main()
{
    message create("Fee","Fi");
    create.append("This is a message...");
   	create.print();

   	mailbox box;
   	box.add_message(create);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//message.cpp
#include <iostream>
#include <string>
using namespace std;
#include "message.h"
#include "mailbox.h"

message::message(string r, string s)
{
   recipient = r;
   sender = s;

}

void message::append(string t)
{
    text = text + t + "\n";
}

string message::to_string() const
{
   return "From: " + sender + "\n" +
      "To: " + recipient + "\n" + text;
}

void message::print() const
{
   cout << to_string();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//message.h
#include <iostream>
#include <string>

using namespace std;
#ifndef MESSAGE_H_
#define MESSAGE_H_

class message {
public:
   message();
   message now();
   message(string r, string s);
   void append(string t);
   string to_string() const;
   void print() const;

private:
   string sender;
   string recipient;
   string text;
};

#endif /* MESSAGE_H_ */ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//mailbox.cpp
#include <iostream>
#include <string>
#include "message.h"
#include "mailbox.h"
using namespace std;

mailbox::mailbox()
{
	box="";
	number=0;
}

void mailbox::add_message(message m)
{
	number++;
	box=box+m.to_string();

}

void mailbox::remove_message(int i)
{

}

string mailbox::get_message(int i)const
{

}

mailbox::~mailbox() {
	// TODO Auto-generated destructor stub
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//mailbox.h
#ifndef MAILBOX_H_
#define MAILBOX_H_
#include <iostream>
#include <string>
using namespace std;
class mailbox
{
public:
	mailbox();
	void add_message(message m);
	string get_message(int i)const;
	void remove_message(int i);
	int number;
	string box;
	virtual ~mailbox();
};

#endif /* MAILBOX_H_ */ 




What's missing?

mailbox's member data is public. You may want to consider making it private.

mailbox's destructor probably doesn't need to be virtual.
Last edited on
In the mailbox.cpp class, I got started under the add_message (message m), but thats as far as I know what to do. I need to finish the remove_message and get_message functions as well. OK, I can change the member data for mailbox, I see what you're saying about that.
I'd expect mailbox to hold a collection of messages. So you need some kind of container in which to store messages.

Your mailbox interface has method string get_message(int i). What is i? Shouldn't it return a message and not a string?
Last edited on
You probably want to use a container such as std::list to store your messages in, rather than using a single string.
For now, all the messages will be packed together into one long string,which is the concatenation of all messages, since we don't yet know how to store a collection of message objects. You can tell where a new message starts by searching for a 'From:' at the beginning of a line. Thats correct, it should be message mailbox::get_message(int i) const;
Last edited on
I see. Are you able to do that? You've described it clearly, coding it should be straightforward from here.
That's where I run into trouble. I dont know how to tie in the message class with the mailbox class. I just mentally don't know how I would retrieve the message from the mailbox.
I take it that box is the string of messages right? add_message might look like:
1
2
3
4
void mailbox::add_message(message m)
{
    box += m.to_string();
}
Alright, I got that much. I need to add a number to the message so when I remove and get the message, it will know which message to get. So how will I implement that? I have a feeling its like:
1
2
3
4
void mailbox::add_message(message m)
{
    box += number+m.to_string();
}


but I can't get it to work. Any suggestions as to where to go?
You need to count them each time I'm afraid.

All this would be trivial if you were allowed to store them in an array of string (std::vector<std::string>).
I asked about it and I am allowed to use an array. Solved the problem easily that way. Thanks for the help and support along the way!
If arrays are allowed, then certainly vector is allowed.

http://www.eptacom.net/pubblicazioni/pub_eng/stroustr.htm
Topic archived. No new replies allowed.