C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 18th, 2005, 01:00 PM
kolokol kolokol is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 18 kolokol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 3 m 5 sec
Reputation Power: 0
Total newbquestion

I have an array of objects of a simple class that stores variables. Ints and single charachters can be stored fine using a[4].letter = 'y'; Where a is the name of the object. I can store multiple charachters e.g. a name into a single object using a("name"), to pass the name as an argument to the constructor, which stores it in a string.
The problem is that i have no idea how to pass an argument such as name to one of an array of objects.
Do i need to provide a default name to the constructor to initialize the array?
Also on a more general note, what is the best approach to store a few variables that i want to use a letter to retrive.
Should i store the letter as part of the object in an array of objects, or should i create an object for each letter and set of variables? If so how do you dynamically supply the object name used to create it?
Alternatively should i use a multidimensional array?
Help and advice much apreciated.

Reply With Quote
  #2  
Old August 18th, 2005, 03:58 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
What do your class and object specifications specifically look like?

As far as multi-dimensional arrays, that depends on style. It will probably be a smiggin faster, but that will be undecernable. The main question is style, and how easy the different methods will be for you to implement and, if necessary, adjust in the future.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old August 18th, 2005, 04:49 PM
kolokol kolokol is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 18 kolokol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 3 m 5 sec
Reputation Power: 0
const int MAXONE = 3;
//declare MAXTHREE
const int MAXTHREE = 5;
//prototype class to read values in amnioacids.txt
//the amino acids class...may put it in an include file later

class AminoAcids
{
public:
AminoAcids(char* pLetter, char* pName)
{
cout << "constructing aminoacid\n" ;
strncpy(letter, pLetter, MAXONE);
letter[MAXONE - 1] = '\0';
strncpy(name, pName, MAXTHREE);
name[MAXTHREE - 1] = '\0';
hp = 0;
}
//more public stuff, these are not protected
char letter[MAXONE];
char name[MAXTHREE];
char hp;
};
//to set the values in main
AminoAcids y("v","val");

all the above works beautifully but as soon as you try to turn y into an array of y objects it breaks. How do i do the above when there are several y objects? y[5] for example?

Reply With Quote
  #4  
Old August 20th, 2005, 09:55 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
When using class objects in arrays you need an default constructor which will be called for each object in the array.

E.g.:
Code:
class CDemo
{
  public:
  CDemo(char cValue)  // preferred constructor
  {
    strcpy(m_cVar, cValue);  // must be strncpy, blabla
  }

  CDemo()                 // 'default' constructor
  {
    memset(m_cVar, 0, sizeof(m_cVar));  // Clear the var
  }

  char m_cVar[80];  // Just for demo
}


For one object you can do:
CDemo y("test");

But for an array the declaration is:
CDemo a[10];
And you see, no option to pass the constructor value. This is why the default constructor is used.

But you can access the functions/vars easilly
strcpy(a[1].m_cVar, "test");

What you often see is the use of pointers to objects in arrays:
CDemo * p_a[10];
p_a[1] = new CDemo("test");
(You need to be carefull with uninitialized pointers though, but hey that's C )

In your (and mine) example there is no real need for a class. You should only use classes when there are specialized functions (te be) written for the data containing the class. But that is the OOP story ...

Reply With Quote
  #5  
Old August 20th, 2005, 06:07 PM
kolokol kolokol is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 18 kolokol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 3 m 5 sec
Reputation Power: 0
Well its not so bad as i first thought. Turns out you can pass arguments to the constructor, but only when you initialize the array. This (i think) defeats the point. If your interesested the working code is below. It seems you need to access it using i as it thinks your trying to create another array. I am now moving on to creating the array with the default constructor as above and specifying the arguments with a function (my first method?).
Thanks for your reply MichaelSoft

AminoAcids array[2]= {
AminoAcids("v","val"),
AminoAcids("t","ter")
};



//display the data
for (int i = 0; i < 2; i++)
{
cout <<array[i].name
<<" "
<<array[i].hp
<<" "
<<array[i].letter
<<endl;
}
Code for the class is unchanged.

Reply With Quote
  #6  
Old August 21st, 2005, 01:51 PM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
You're welcome

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Total newbquestion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT