473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to store data in a different class object?

33
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!
Oct 22 '09 #1
8 3293
Banfa
9,065 Expert Mod 8TB
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.
Oct 22 '09 #2
hanna88
33
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.
Oct 22 '09 #3
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.
Oct 23 '09 #4
hanna88
33
hi.thank you for your reply.

@sumeshnb
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?
Oct 23 '09 #5
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.
Oct 24 '09 #6
hanna88
33
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
Oct 25 '09 #7
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);
}
Oct 25 '09 #8
hanna88
33
i'll try to understand the code.u've been a great help! thank you . :D
Oct 26 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Lars Behrens | last post by:
Hi there! For a web project I need a little expert help. I don't have written much code yet, just been fiddling around a bit, testing and planning. The web site will have a submission page for...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
7
by: Jenny | last post by:
Hi, I have a class foo which will construct some objects in my code. some of the objects store int values into the data deque, while others store float values to the deque. template <class...
3
by: pei_world | last post by:
hi I am new to C# programming. can anyone tell me what is the standard way to store high sensitive user data for application, so that application next run can get back those data.
17
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
3
by: ExclusiveResorts | last post by:
Can the CallContext be used reliably for storing request specific data? We are developing an application library that uses the CallContext to keep an IdentityMap (hashtable of business objects...
10
by: Craig Lister | last post by:
I'm a newbie.. I'm trying to list all files in a directory, and store them in a aFile object. As I iterate through each file, I store the size, name, path, date etc... How do I store them...
0
by: Nata | last post by:
hi all i am trying to store the XML Document object into MS SQL2005 i want to store this as whole not different tables; plz help me regarding this i am trying form last two weeks i asked many...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.