Connecting Tech Pros Worldwide Help | Site Map

how to store data in a different class object?

Member
 
Join Date: Oct 2009
Posts: 33
#1: 4 Weeks Ago
hello..
is there a way of storing data to other class object.
here's the problem.
class Repository is where it reads through the sentences and tokenize it
into words . and what i did is by using strtok i keep all the tokenize words into a vector.and planning to store this vector of word into class Word object.

im not sure whether this is a good idea because it seems hard to store a list of vector into an object of different class.

any help would be great! thanks!
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,149
#2: 4 Weeks Ago

re: how to store data in a different class object?


Are you planing on storing the whole vector in a single instance of the Word class or are you planning to store each entry in the vector in a separate instance of the class?

Either way it is a fairly simple job to just create your Word class with a constructor that takes the right data.
Member
 
Join Date: Oct 2009
Posts: 33
#3: 4 Weeks Ago

re: how to store data in a different class object?


but how should i indicate the word should be stored in class Word constructor.
will this do .. Word* p where p is the tokenize word and keep updated in a while loop for strtok. thanks for your input.
Newbie
 
Join Date: Feb 2009
Location: Chennai, India
Posts: 8
#4: 4 Weeks Ago

re: how to store data in a different class object?


Hi,
There are many ways in which you can store data into other class object, Bafna already gave you one way.
you just define your other calss constructor like following:
Word::Word(verctor<words> &vwords)
{
m_vectorWords = vwords;//m_verctorWords is a data member of type
// vector<words>
}

One another possible way could be(In case the Word object already exisitng) implement a interface function in otherclass like following:

Word::setVectorWord(vector<word> &vw)
{
m_vectorWords = vw;//m_verctorWords is a data member of type
// vector<words>
}

and call this interface function from your Repository class after tokenizing and putting the words in to token.
Member
 
Join Date: Oct 2009
Posts: 33
#5: 4 Weeks Ago

re: how to store data in a different class object?


hi.thank you for your reply.

Quote:

Originally Posted by sumeshnb View Post


Word::setVectorWord(vector<word> &vw)
{
m_vectorWords = vw;//m_verctorWords is a data member of type
// vector<words>
}

and call this interface function from your Repository class after tokenizing and putting the words in to token.

just quick que.. is &vw is where i have the tokenize word in this case?
i was given a constructor Word(string word); i reckon i cant just passed vector<word> into this constructor.so if i do it this way,will the data be passed to the Word object since i didnt declare it in a constructor?
Newbie
 
Join Date: Feb 2009
Location: Chennai, India
Posts: 8
#6: 4 Weeks Ago

re: how to store data in a different class object?


Yes in "vector<word> &vw" you have the words tokenized.

From your reply I understand your constructor of Word looks like
Word::Word(string word), If that is the case you may have to write a new constructor..
You cannot pass vector<word> to your existing constructor...!

If the explanation is not satisfactory, please give more details of the classes with examples.
Member
 
Join Date: Oct 2009
Posts: 33
#7: 4 Weeks Ago

re: how to store data in a different class object?


hi.this is the h.file for the code.

Expand|Select|Wrap|Line Numbers
  1. class Word {
  2.  
  3.         private:
  4.             std::string word;
  5.  
  6.         public:
  7.         Sentence *getSentence();
  8.         void setSentence(Sentence *sentence);
  9.         Word(string word); 
  10.         virtual string getKey() { return word;};
  11.         virtual ~Word();
  12.         virtual string asString();
  13.         __declspec( property(get = getSentence, put = setSentence))
  14.         Sentence *mySentence;
  15. };
  16.  
  17. class Sentence {
  18.  
  19.         public:
  20.         Document *getDocument();
  21.         void setDocument(Document *document);
  22.         Sentence();
  23.         virtual ~Sentence();
  24.         virtual string asString();
  25.         __declspec( property(get = getDocument, put = setDocument))
  26.         Document *myDocument;
  27. };
  28.  
  29. class Document {
  30.  
  31.         private:
  32.         string documentId;
  33.  
  34.         public:
  35.         Repository *getRepository();
  36.         void setRepository(Repository *repository);
  37.         Document(string documentId);
  38.         virtual ~Document();
  39.         virtual string getKey() { return documentId;};
  40.         virtual string asString();
  41.         __declspec( property(get = getRepository, put = setRepository))
  42.         Repository *myRepository;
  43. };
  44.  
  45. class WordCollection : public vector<Word*> {
  46.  
  47.         public:
  48.         string asString() {
  49.         string result;
  50.         WordCollection::iterator iter = begin();
  51.  
  52.         for (; iter != end(); iter++) {
  53.             result += (*iter)->getKey() + "|" +
  54.             (*iter)->mySentence->asString() + "|" +
  55.             (*iter)->mySentence->myDocument->getKey() + "\n";}
  56.         return result;
  57.         }
  58. };
  59.  
  60. class Repository {
  61.  
  62.         public:
  63.         virtual ~Repository();
  64.         string asString();
  65.         Document *addDocument(iostream& inStream, string documentId);
  66.         void clear();
  67.         void search(const string& keyword, WordCollection& coll);
  68. };
  69.  
i try to start by doing the function below where i did convert it to string using getline and try to tokenize the str.

Expand|Select|Wrap|Line Numbers
  1. Document *addDocument(iostream& inStream, string documentId);
  2.  
here' the problem.i dont understand how can i store the tokenize word into Class Word.same goes for sentences which will be store in Class Sentence if i tried adding all the tokenize word into vector.
the other problem is how can i store the Word pointers in each Sentence object and etc.

any examples would be really great.
thanks
Newbie
 
Join Date: Feb 2009
Location: Chennai, India
Posts: 8
#8: 4 Weeks Ago

re: how to store data in a different class object?


If I understand your problem correctly , I would write the
function
Document *addDocument(iostream& inStream, string documentId);
as follows:



Document *Repository::addDocument(iostream& inStream, string documentId)
{

char line[250];
inStream.getline(line,250);

char * ptrToToken = NULL;
ptrToToken = strtok (line," ,.-");//assumes words are separated
//by spaces,comma,dot or hyphen

Sentence * ptrToSent = new Sentence();

Document * ptrToDoc = new Document(documentId);

ptrToSent -> setDocument(ptrToDoc);
ptrToDoc ->setRepository(this);

while (ptrToToken != NULL)
{
Word * ptrToWord = new Word(ptrToToken);
ptrWord -> setSentence(ptrToSent);
ptrToScent -> addWord(ptrToWord)
ptrToToken = strtok (NULL, " ,.-");
}


return ptrToDoc;
}




Before writing the above function , I assume there is a
data member and member function in Sentence class like the following :

Data member:
WordCollection wordColl;

member function:
Sentence::addWord(Word * ptrToWord)
{
wordColl.push_back(ptrToWord);
}
Member
 
Join Date: Oct 2009
Posts: 33
#9: 3 Weeks Ago

re: how to store data in a different class object?


i'll try to understand the code.u've been a great help! thank you . :D
Reply