Array of Objects.

This is a discussion on Array of Objects. within the C++ Programming forums, part of the General Programming Boards category; How would I create an array of objects (x number of Electrons around a Nucleus)? I have this code thus ...

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Array of Objects.

    How would I create an array of objects (x number of Electrons around a Nucleus)? I have this code thus far:

    Code:
    class Nucleus
    {
    
    	Electron * eTron;
    
    	// Co-ordinates of the WHOLE atom in 3D space
    	float x, y, z;
    
    	Nucleus(int e)
    	{
    		eTron = new Electron [e];
    	}
    
    };
    
    class Electron
    {
    	// Location around Nucleus
    	float x, y, x;
    
    	Electron()
    	{
    
    	}
    
    };
    Is this correct or am I missing something?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well the Electron class would need to be declared before the Necleus class but yes that seems like a correct outline.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is this correct or am I missing something?
    Experimentation is the spice of programming. Learn to savor its sweet bouquet, to luxuriate in its sharp yet sweet flavor. Only then can you lose yourself in the ecstasy that is programming.

    My best code is written with the delete key.

  4. #4
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    If Electron had a constructor that neede values to be passed, how would I call it using the New keyword:

    Code:
    Electron(int orbit, int x, int y, int z)
    {
    ......
    }
    
    
    
    Electron *eTron = new Electron(parameters???)[e];
    ??

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I believe that you can only use a default constructor with new. So if Electron needed certain information passed into it you would have to use default parameters. But I may be wrong.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how would I call it using the New keyword
    You don't. Not that way at least. If you want to use anything but the default constructor then you need to do something different, such as using an array of pointers and then allocating memory in a loop to each object, or using the object's assignment operator to give it a new object using the constructor:
    Code:
    eTron = new Electron [e];
    for ( int i = 0; i < e; i++ )
      eTron[i] = Electron ( /* parameters */ );
    My best code is written with the delete key.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    have electron take in a const nucleus, so i t knows which one it belongs to. or if you only using one nucleus in the program, have electrons class use static variable to count how many electrons created
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    >Experimentation is the spice of programming. Learn to savor its sweet bouquet, to luxuriate in its sharp yet sweet flavor. Only then can you lose yourself in the ecstasy that is programming

    Wow prelude! My new favorite quote.

    [edit]
    You make programming sound better then chocolate.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    You make programming sound better then chocolate.
    Now you're stepping on very thin ice indeed

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You make programming sound better then chocolate.
    Nothing is better than chocolate.
    My best code is written with the delete key.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i think an array of preludes would be better than chocolate
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Well I got this code kinda working. I get a distructor call after I have the pointer pointing to a new electron. LoadElectron Gets errors when it tries to access variables inside the electron class because it doesnt exist anymore. Am I doing something wrong?

    Code:
    class Atom
    {
      Electron *eTron;
    
      Arom()
      {
    
        for (int x = 0; x < e; x++)
        {
      	// Create Electron Orbit
      	eTron[x] = Electron();
      
        //////////////////////// DISTRUCTOR CALL ////////////////////////   
    
      	eTron[x].LoadElectron(x);
        }
      }
    };

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Did you allocate eTron using new? And you need to define an assignment operator for the electron class to do that...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21