GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Nov-2006, 13:24
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 108
TransformedBG is on a distinguished road

Message Class


Quote:
Assignment:
Implememnt the message class using a a header and implemtin a file name message.h and message.cpp respectively. In addition to the two message class files you must write a driver. The driver mus creat a message array that is capable of holding 1000 messages (make sure that you dont exceed the the maximum number of messages) It then must read all Messages from a file named Messages.txt; sort the messages based on the date, fand a message bassed on the messageID(value will be given by keyboard input), and print the first five messages and the message resulting from the search.

Given:
Memeber Functions :


Message()
- Default Constructor that increments the messageID

Message(string, string string)
- Constructor twith initiallization strings for sender, recipient, and body, it will also increment the messageID.

~Message()
-Destructor

void setSender(string)
- Modifier that allows the user to set the sender string

void setRecipient(string)
- Modifier that allows the user to set the recipient

void setBody(string)
- Modifier that allows the user to set the Body

void setDate(string)
- Modifier that allows the user to set the date

string getSender()
- Accessor that allows the user to get the sender string

string getRecipient()
- Accessor that allows the user to get the recipient string

string getBody()
- Accessor that allows that user to get the Body string

string getDate()
- Accessor that allows the user to get the date string

static in getMessageID()
- Accessor that allows the user to get the value of message ID

string toString()
- Function that returns a string representation of a message object.

exampel:
messageID: 1
date: 2006/11/20
sender: bobbie@baylor.edu
recipient: lilley@baylor.edu
body: Hope your semester is going well!

k not sure what do do on the toString one but so far this is what i have:

Message.h
CPP / C++ / C Code:
#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <sstream>
using namespace std;

class Message
{ 
	private:
		string sender;
		string recipient;
		string body;
		string date;
		static int messageIDGen;
		int messageID;

    public:
		Message(){ messageID = ++messageIDGen; }
		Message(string, string, string, string);
		string getStringMessageID();
		void setSender(string);		
		void setRecipient(string);
		void setBody(string);
		void setDate(string);
		string getSender();
		string getRecipient();
		string getBody();
		string getDate();
		string toString();
};
#endif


Message.cpp
CPP / C++ / C Code:
#include "Message.h"

void Message::setSender(string Sender)
{
	sender=Sender;
}

void Message::setRecipient(string Recipient)
{
	recipient=Recipient;
}

void Message::setBody(string Body)
{
	body=Body;
}

void Message::setDate(string Date)
{
	date=Date;
}

string Message::getSender()
{
	return sender;
}


string Message::getRecipient()
{     
	return recipient;
}


string Message::getBody()
{     
	return body;
}


string Message::getDate()
{     
	return date;
}

string Message::toString()
{
	// need something to return ;
}


string Message::getStringMessageID()

{
	ostringstream temp;
	temp << messageID;      
	return temp.str();      
}

and i havent writen the driver yet. but neet help with the toString

Oh and what is a destructor?
Last edited by LuciWiz : 30-Nov-2006 at 05:27. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 29-Nov-2006, 15:51
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 108
TransformedBG is on a distinguished road

Re: Message Class


anyone? ???
  #3  
Old 29-Nov-2006, 15:55
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Message Class


For toString, you need to figure out what a string representation of a Message object is. It could be something like this
Quote:
Message from {Sender} to {Recipient} on {Date}: {Body}
or something completely different. You can just concatenate these into one giant string and return it. If you need to put multiple data types into the string (like the message ID) use functions like itoa() or stringstreams.

A destructor is a function called as your class is going out of existence (at the end of the scope* in which it was declared, or for dynamically allocated objects, when delete is called). The destructor should clean up any remaining loose ends in the class. If the class holds pointers to any dynamically allocated memory (like a C-style char* allocated with new[]) then it should release the memory to prevent memory leaks. Likewise, if the class holds a handle to a resource (like a file or an internet connection or whatever) it should close the handle for the same reason.

The destructor takes no parameters and returns nothing. It is declared like this:
CPP / C++ / C Code:
class MyClass
{
     public:
...
           ~MyClass();
};

MyClass::~MyClass()
{

}
Then you implement it like a regular member function (of course you could also put the declaration inline in the class definition like always). One thing you cannot do in a destructor is thrown an exception and allow it to leave the destructor. The reasons for this are complex**, but the end result is that if a destructor throws an exception and it is not caught within the destructor function, the application will immediately crash.

*The scope is basically anything between { and }. Variables inside a scope are destroyed when the scope ends, and are not visible from outside it. So a function has its own scope, but so does an if statement within the function, loops, etc. Google can provide you with a more detailed explanation.

**Here's the complex explanation. The problem is that if an exception is thrown from anywhere, the stack begins to unroll. This is basically walking backwards in time, back through any function calls. As this happens, scopes are exited and any classes declared within those scopes are destructed. Now, what happens if you throw an exception while an exception is propagating? Well, the designers of C++ couldn't find an acceptable solution, so they decided the answer is CRASH.
  #4  
Old 29-Nov-2006, 16:32
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 108
TransformedBG is on a distinguished road

Re: Message Class


So destructors pass no paramaters? k... could i get an example of just a basic program driver for a destructor?

And the toString Message should be everything bundled up i belive? but the functions you spoke about i havent learned yet, so maybe i could make a c-string?
  #5  
Old 29-Nov-2006, 20:13
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 108
TransformedBG is on a distinguished road

Re: Message Class


I havent finished this but im kind of stuck.. i need a little help still on the toString Function still. and the driver.

So far i have something like this:
CPP / C++ / C Code:
#include "Message.h"

int main()
{
	int messageID;
	string extra;
	Message message;

	cout << "Which messageID would you like to view? ";
	cin >> messageID;
	
	
	
	ifstream inFile;
	inFile.open("messages.txt");
	if (!inFile)
	{
		cout << "File not found." << endl;
		return 0;
	}



	for(int messages = 0; messages <=999; messages++)
	{
			inFile >> message.getDate;
			inFile >> message.getRecipient;
	}
	cout << endl << "Your message is:" << endl;
	cout << endl << "messageID:" << "\t" << messageID;
	cout << endl << "date:" << "\t";
	cout << endl << "sender:" << "\t";
	cout << endl << "recipient:" << "\t";
	cout << endl << "body:" << "\t";
	return 0;
}



I want the end result to look like:
Quote:
Which messageID would you like to view?
Your message is:
messageID: 2
date: 2061/11/19
sender: bobbie@baylor.edu
recipient: kelley@baylor.edu
body: Thanksgiving holidays begin this week. Get your work done before leaving for home.

messageID: 4
date: 2003/05/23
sender: bobbie@baylor.edu
recipient: aars@baylor.edu
body: Hope the short break before summer classes is going good.

messageID: 3
date: 2005/12/23
sender: aars@baylor.edu
recipient: bobbie@baylor.edu
body: Merry Christmas!

messageID: 1
date: 2006/11/20
sender: bobbie@baylor.edu
recipient: lilley@baylor.edu
body: Hope your semester is going well!

messageID: 6
date: 2006/11/20
sender: bobbie@baylor.edu
recipient: lilley@baylor.edu
body: Hope your semester is going well!

messageID: 5
date: 2006/12/03
sender: 1430class@baylor.edu
recipient: aars@baylor.edu
body: This is a message from the ghost of Christmas Yet to Come.
  #6  
Old 29-Nov-2006, 21:28
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 108
TransformedBG is on a distinguished road

Re: Message Class


Current:

message.h
CPP / C++ / C Code:
#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


/**************************************************************************
*
*	class: Message
*	Discription: A class that stores incoming emails and sorts them 
*
***************************************************************************/
class Message
{ 
	private:
		string sender, recipient, body, date;  
		static int messageIDGen;
		int messageID;

    public:
/**************************************************************************
*
*	class: Message
*	Function: Message
*   Description: Default constructor
*
***************************************************************************/
		Message(){ messageID = ++messageIDGen; };
	

/**************************************************************************
*
*	class: Message
*	Function: Message
*   
*
***************************************************************************/
		Message(string, string, string, string);

/**************************************************************************
*
*	class: Message
*   Function: ~Message
*	Description: Destructor
*
***************************************************************************/
		~Message();


/****************************************************************
*
*	Class: Message
*   Function: setSender
*   Description: Modifier that allows the user to set the 
*   sender string
*
****************************************************************/
		void setSender(string);

/****************************************************************
*
*	Class: Message
*   Function: setRecipient
*   Description: Modifier that allows the user to set the 
*   recipient
*
****************************************************************/
		void setRecipient(string);

/****************************************************************
*
*	Class: Message
*   Function: setBody
*   Description: Modifier that allows the user to set the Body
*
****************************************************************/
		void setBody(string);

/****************************************************************
*
*	Class: Message
*   Function: setDate
*   Description: Modifier that allows the user to set the date
*
****************************************************************/
		void setDate(string);

/****************************************************************
*
*	Class: Message
*   Function: getSender
*   Description: Accessor that allows the user to get the 
*   sender string
*
****************************************************************/
		string getSender();

/****************************************************************
*
*	Class: Message
*   Function: getRecipient
*   Description: Accessor that allows the user to get the 
*   recipient string
*
****************************************************************/
		string getRecipient();

/****************************************************************
*
*	Class: Message
*   Function: getBody
*   Description: Accessor that allows that user to get the 
*   Body string
*
****************************************************************/
		string getBody();

/****************************************************************
*
*	Class: Message
*   Function: getDate
*   Description: Accessor that allows the user to get the
*   date string
*
****************************************************************/
		string getDate();

/****************************************************************
*
*	Class: Message
*   Function: toString
*   Description: Function that returns a string representation 
*   of a message object
*
****************************************************************/
		string toString();
		
/****************************************************************
*
*	Class: Message
*   Function: getStringMessageID
*   Description: We have no idea what this does lol
*
****************************************************************/
		string getStringMessageID();

};



#endif

Message.cpp
CPP / C++ / C Code:
#include "Message.h"

int Message::messageIDGen = 0;

/****************************************************************
*
*	Class: Message
*   Function: Message
*   Description: contructor
*
****************************************************************/
Message::Message(string Date, string Sender, string Recipient, string Body)
{	

	messageID = ++messageIDGen;
	date = Date;
	sender = Sender;
	recipient = Recipient;
	body = Body;
}

/****************************************************************
*
*	Class: Message
*   Function: ~Message
*   Description: Destructor
*
****************************************************************/
Message::~Message()
{

}

/****************************************************************
*
*	Class: Message
*   Function: setSender
*   Description: Modifier that allows the user to set the 
*   sender string
*
****************************************************************/
void Message::setSender(string Sender)
{
	sender=Sender;
}

/****************************************************************
*
*	Class: Message
*   Function: setSender
*   Description: Modifier that allows the user to set the 
*   recipient
*
****************************************************************/
void Message::setRecipient(string Recipient)
{
	recipient=Recipient;
}

/****************************************************************
*
*	Class: Message
*   Function: setBody
*   Description: Modifier that allows the user to set the Body
*
****************************************************************/
void Message::setBody(string Body)
{
	body=Body;
}

/****************************************************************
*
*	Class: Message
*   Function: setDate
*   Description: Modifier that allows the user to set the date
*
****************************************************************/
void Message::setDate(string Date)
{
	date=Date;
}

/****************************************************************
*
*	Class: Message
*   Function: getSender
*   Description: Accessor that allows the user to get the 
*   sender string
*
****************************************************************/
string Message::getSender()
{
	return sender;
}

/****************************************************************
*
*	Class: Message
*   Function: getRecipient
*   Description: Accessor that allows the user to get the 
*   recipient string
*
****************************************************************/
string Message::getRecipient()
{     
	return recipient;
}

/****************************************************************
*
*	Class: Message
*   Function: getBody
*   Description: Accessor that allows that user to get the 
*   Body string
*
****************************************************************/
string Message::getBody()
{     
	return body;
}

/****************************************************************
*
*	Class: Message
*   Function: getDate
*   Description: Accessor that allows the user to get the
*   date string
*
****************************************************************/
string Message::getDate()
{     
	return date;
}

/****************************************************************
*
*	Class: Message
*   Function: toString
*   Description: Function that returns a string representation 
*   of a message object
*
****************************************************************/
string Message::toString()
{
	//use the function getStringMessageID, the members of the class
	string result = "Message #" + getStringMessageID() 
	+ "\nDate: " + date + "\nSender: " + sender + "\nRecipient: " 
	+ recipient + '\n' + body; //and then the body of the message

	return result;
}

/****************************************************************
*
*	Class: Message
*   Function: getStringMessageID
*   Description: We have no idea what this does lol
*
****************************************************************/
string Message::getStringMessageID()

{
	ostringstream temp;
	temp << messageID;      
	return temp.str();      
}





New driver:
CPP / C++ / C Code:
#include "Message.h"

int main()
{
	int messageID;
	string current_line="";
	Message messages[1000];

	//ask and promps user for a message to view
	cout << "Which messageID would you like to view? "; 
	cin >> messageID;
	
	//declares an instream file
	ifstream inFile;
	inFile.open("messages.txt"); //opens messsages.txt
	
	//checks to see if file is found, and displays error if not
	if (!inFile)
	{
		cout << "File not found." << endl;
		return 0;
	}


	//allows only 1000 messages to be entered
	for(int i = 0; i <=999; i++)
	{
		getline(inFile,current_line);
		messages[i].setDate(current_line);

		getline(inFile,current_line);
		messages[i].setSender(current_line);

		getline(inFile,current_line);
		messages[i].setRecipient(current_line);

		getline(inFile,current_line);
		messages[i].setBody(current_line);
	}
	

	
	cout << endl << "Your message is:" << endl;
	cout << endl << "messageID:" << "\t" << messageID;
//	cout << endl << "date:" << "\t" << messages.getDate();
//	cout << endl << "sender:" << "\t" << messages.getSender();
//	cout << endl << "recipient:" << "\t" << messages.getRecipient();
//	cout << endl << "body:" << "\t" << messages.getBody();
	return 0;
}


 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Box Class, need help again :( TransformedBG CPP / C++ Forum 7 13-Nov-2006 15:11
Card class help verticalvoid CPP / C++ Forum 2 29-Jun-2006 21:51
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 CPP / C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.