Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,920 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,991 people online right now. Registration is fast and FREE... Join Now!




array of objects and parameterised constructor

 
Reply to this topicStart new topic

array of objects and parameterised constructor

dan_ram
20 Jan, 2008 - 09:14 AM
Post #1

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 88


My Contributions
wat is the syntax for creatin an object for a class with parameters AND object arrays?
tis is a part of the pgm:
CODE

RUNS(char n[21],int a,int si,int f)
{
   strcpy(name,n); //variables hav already been declared  
   age=a;
   sixes=si;
   fours=f;
}

CODE

int main()
{
    RUNS s("unavailable",0,0,0);

as i need the details of more than 1 player, i will hav 2 use array of objects where the syntax would b
CODE

RUNS s[11];

the problem now is that i dunno how 2 make both possible at the same time..

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Array Of Objects And Parameterised Constructor
20 Jan, 2008 - 11:09 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well... the first bit of code there looks like a function not an object... I will assume that it is your constructor?

In that case:

First I would like to warn you against using arrays of objects -- look into the use of vectors.

Secondly, if you have a default constructor that requires no arguments you will not have a problem creating large numbers of objects

CODE
class foo {
      public:
             char name[100];
             int charAge;
            
             foo(const char *n, const int age)
             {
                      strcpy(name,n);
                      charAge = age;
             }
            
             foo()
             {
                  name[0]=0x00;
                  charAge = 0;
             }
                      
};

int main() {
    foo a("Hello", 21);
    foo arr[2];
    return 0;  
}


If you don't have a constructor (and there is no base class, all non-static member are public, and you have no abstract methods) then you can use the struct-array initialization syntax:

CODE
class foo {
      public:
             char name[100];
             int charAge;
};

int main() {
    foo arr[2] = {
        {"One", 1},
        {"Two", 2}
        };
    return 0;  
}


I don't recommend this method as the conditions can be a little hard to keep track of (I had to look them up myself).
User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Array Of Objects And Parameterised Constructor
21 Jan, 2008 - 07:39 AM
Post #3

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 88


My Contributions
thnx.....
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month