how to store data in a different class object?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanna88
    New Member
    • Oct 2009
    • 33

    how to store data in a different class object?

    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
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • hanna88
      New Member
      • Oct 2009
      • 33

      #3
      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.

      Comment

      • sumeshnb
        New Member
        • Feb 2009
        • 13

        #4
        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(verc tor<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::setVector Word(vector<wor d> &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.
        Last edited by sumeshnb; Oct 23 '09, 07:08 AM. Reason: class name changed

        Comment

        • hanna88
          New Member
          • Oct 2009
          • 33

          #5
          hi.thank you for your reply.

          Originally posted by sumeshnb

          Word::setVector Word(vector<wor d> &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?

          Comment

          • sumeshnb
            New Member
            • Feb 2009
            • 13

            #6
            Yes in "vector<wor d> &vw" you have the words tokenized.

            From your reply I understand your constructor of Word looks like
            Word::Word(stri ng 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.

            Comment

            • hanna88
              New Member
              • Oct 2009
              • 33

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

              Code:
              class Word {
              
              		private:
              			std::string word;
              
              		public:
              		Sentence *getSentence();
              		void setSentence(Sentence *sentence);
              		Word(string word); 
              		virtual string getKey() { return word;};
              		virtual ~Word();
              		virtual string asString();
              		__declspec( property(get = getSentence, put = setSentence))
              		Sentence *mySentence;
              };
              
              class Sentence {
              
              		public:
              		Document *getDocument();
              		void setDocument(Document *document);
              		Sentence();
              		virtual ~Sentence();
              		virtual string asString();
              		__declspec( property(get = getDocument, put = setDocument))
              		Document *myDocument;
              };
              
              class Document {
              
              		private:
              		string documentId;
              
              		public:
              		Repository *getRepository();
              		void setRepository(Repository *repository);
              		Document(string documentId);
              		virtual ~Document();
              		virtual string getKey() { return documentId;};
              		virtual string asString();
              		__declspec( property(get = getRepository, put = setRepository))
              		Repository *myRepository;
              };
              
              class WordCollection : public vector<Word*> {
              
              		public:
              		string asString() {
              		string result;
              		WordCollection::iterator iter = begin();
              
              		for (; iter != end(); iter++) {
              			result += (*iter)->getKey() + "|" +
              			(*iter)->mySentence->asString() + "|" +
              			(*iter)->mySentence->myDocument->getKey() + "\n";}
              		return result;
              		}
              };
              
              class Repository {
              
              		public:
              		virtual ~Repository();
              		string asString();
              		Document *addDocument(iostream& inStream, string documentId);
              		void clear();
              		void search(const string& keyword, WordCollection& coll);
              };
              i try to start by doing the function below where i did convert it to string using getline and try to tokenize the str.

              Code:
              Document *addDocument(iostream& inStream, string documentId);
              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

              Comment

              • sumeshnb
                New Member
                • Feb 2009
                • 13

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



                Document *Repository::ad dDocument(iostr eam& inStream, string documentId)
                {

                char line[250];
                inStream.getlin e(line,250);

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

                Sentence * ptrToSent = new Sentence();

                Document * ptrToDoc = new Document(docume ntId);

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

                while (ptrToToken != NULL)
                {
                Word * ptrToWord = new Word(ptrToToken );
                ptrWord -> setSentence(ptr ToSent);
                ptrToScent -> addWord(ptrToWo rd)
                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::addWo rd(Word * ptrToWord)
                {
                wordColl.push_b ack(ptrToWord);
                }

                Comment

                • hanna88
                  New Member
                  • Oct 2009
                  • 33

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

                  Comment

                  Working...