Thread: Newbie question on Structures

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    Newbie question on Structures

    Hiya!

    (Optional reading as I introduce myself) I'm a Java Programmer with a couple years of experience, teaching myself how to write C++. I've got a pretty good handle on most of the basic concepts that are common between Java and C++, and I generally learn best by doing rather than reading.(/Introduction off).

    My currect project is to create a "Galaxy" of random types of stars, each containing a system of planets of variable type and size.

    I currently have code that functions well that creates the stars by color and sorts them. But to progress further, I am stuck. At some point later I will be assigning each of these stars a random name, but haven't implemented that yet.

    My question: I was thinking, that the easiest way for me to do this effectively would be first to create a Structure Planet which would contain variables Planet Size and Planet Envrionment, and then create another Structure Starsystem which would contain a variable number of the Planet Structures.

    Is this legal in C++? To place one Structure within another Structure? For example...

    Code:
                Struct Planet
                {
                   int planetsize;
                   int planetenv;
                 }
    
                Struct Starsystem
                {
                  int num_planets;
                  Planet insystem[10];
                 }
            
                 Starsystem sol;
                 sol.num_planets = 9;
    Is this legal? If so, how does one access the Structure inside of the Structure?

    Would this work?

    Code:
    sol.insystem[2].size = 100;
    TIA, Structures are pretty new to me in concept and I'm trying to get a grasp of how to use them.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Would this work?

    Code:

    Code:
    sol.insystem[2].size = 100;
    Yes.

    You're on the right path as far as setting this up. Looks good.

    Welcome to the boards!

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Ty very much!

    I appriciate it alot

    Glad to see I'm on the right path For a little while there I thought I was baking my brain trying to teach myself!

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Wait a minute..."Struct"? ....C++ is type sensitive....it has to be "struct".

    Or is that just psuedo-code?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    TIA, Structures are pretty new to me in concept and I'm trying to get a grasp of how to use them.
    Why? C++ is about using classes not structures. The good news is that the only difference between structures and classes is the default access: in classes if you don't declare the access: e.g. public, private, then it is private by default. With structs the default access is public.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Quote Originally Posted by Gatt9

    Would this work?

    Code:
    sol.insystem[2].size = 100;
    probably not, you dont have a size variable in Planet - its planetsize.
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I think what your doing there is also called inheritance

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Quote Originally Posted by 7stud
    Why? C++ is about using classes not structures
    True. Somewhere I read that struct was only there for backward compatibility with C. But in practice, structures usually don't contain member functions... that is, the struct keyword is normally used for C-style structures.

    I don't recommend that people learn C before learning C++ (like I did), but understanding C-style structrues first made classes & objects much easier to understand.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but understanding C-style structrues first made classes & objects much easier to understand.
    Java programmers are well aware of what classes and objects are since everything in Java is a class--even main() must be inside a class in Java.

  10. #10
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Welshy
    I think what your doing there is also called inheritance
    Not quite. Inheritance is a little different.

  11. #11
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by homeyg
    Not quite. Inheritance is a little different.
    surely it's basic inheritance?

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Ty all!

    Yes, that was just Psuedo-code, I figured as I was writing the thread I'd better come up with some code to demonstrate what I was think of doing

    Java is pretty class based, in Java I'd have done this differently. C++ seems more flexible though, to do this in Java I'd have written one or two clases with multiple methods to do what it seems I can do with structures in a dozen lines in C++.

    Anyways, I'm off to implement this and see how things work out

    Ty all again!

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Welshy
    surely it's basic inheritance?
    Yes. But inheritance deals with classes and needs a special sytax. (I think it has a few hidden things that need attention like the behavior of the constructors in the classes).

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Gatt9
    Ty all!
    ...to do this in Java I'd have written one or two clases with multiple methods to do what it seems I can do with structures in a dozen lines in C++.

    Anyways, I'm off to implement this and see how things work out

    Ty all again!
    <sigh>
    As someone posted, structures are for backwards compatibility with C. I thought you aspired to be a C++ programmer?

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No that's not basic inheritance.
    Inheritance is inherting traits from a class to another class not having the class as a data member in you class.
    7stud I use structs all of the time. It's a good way to seperate things that have member functions etc. from things that just are data only. But as was said they do the exact same thing, so it is just a matter of preference.
    Last edited by prog-bman; 03-25-2005 at 11:30 PM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 09:58 PM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM