Structures? *brain melts*

This is a discussion on Structures? *brain melts* within the C++ Programming forums, part of the General Programming Boards category; I was reading the section of the tutorial on structures, and man I'm so lost. Maybe it's because I'm trying ...

  1. #1
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53

    Structures? *brain melts*

    I was reading the section of the tutorial on structures, and man I'm so lost. Maybe it's because I'm trying to compare structures in C++ to modules in QBasic.

    In Qbasic, a module initialized like so:

    Code:
    : Gosub Module1
    :
    : Module1:
    :    PRINT "This is a module"
    :    RETURN
    See, to me, I see a structure in C++ as a module... I don't understand it in any other way. In the tutorial, it gives this example:

    Code:
    struct example
    {
          int x;
    };
    example an_example;  //Treating it like a normal variable type
    an_example.x=33;     //How to access it
    I was thinking for a while that structures were a sort of "bucket" for a certain specified number of variables. But then, in the last line, where it says "an_example.x=33" why is it doing that? I figure all you would have to do is example.x=33 to set the variable "X" in structure "Example" to a value! Why do they go through the trouble to change the name of example to "an_example" and then change the value of X? It's all so confusing, I'm losing my brain through my ears! Someone help! Thanks!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    struct example
    {
          int x;
    };
    example an_example;  //Treating it like a normal variable type
    an_example.x=33;     //How to access it
    Same as

    Code:
    type Example
      x as integer
    end type
    
    dim shared anexample as Example
    anexample.x=33
    Just as in BASIC example is only a structure...it does not exist until you create an object of that type.

    In C:
    example MyExample;

    In QB:
    dim shared MyExample as example




    But BASIC's types are limited in that they cannot have variable length arrays, pointers (cuz pointers suck in BASIC), or functions associated with them.

  3. #3
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Well I don't know much of QBasic either lol. I'm in a Intro to Computer Programming class in college, and we just got done learning modules. So, that honestly didn't make any sense. Feel free to try again lol.


    My mistake, I shouldn't have brought up Qbasic!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    All hail the glory of C++!

    Classes are also similar.

    Code:
    class Example
    {
    public:
          int x;
    };
    
    Example an_example;
    an_example.x=33;
    Classes are better than structs, although right now I've forgotten why.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You are confusing modules with structures. Modules are files of code. In QB you can have multiple modules and in C you can have multiple modules.


    For instance a project might look like this:


    graphics.cpp
    main.cpp
    sound.cpp
    input.cpp
    - joystick.cpp
    - mouse.cpp
    - keys.cpp

    Each of those is a module. The compiler/linker create an executable out of the whole thing.

    But modules have nothing to do with structs.

  6. #6
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    All hail the glory of C++!

    Classes are also similar.

    Code:
    class Example
    {
    public:
          int x;
    };
    
    Example an_example;
    an_example.x=33;
    Classes are better than structs, although right now I've forgotten why.

    Sorry... I didn't catch your point.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  7. #7
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    I just wanna know why it changes "example" to "an_example"

    And perhaps an example of how a structure would be used to get a certain result or something, that might help out too! Thanks for working with me this far! lol...
    --MrDoomMaster
    The kind of DooM that makes the MooD

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    class Example
    {
    public:
          int x;
    };
    
    Example an_example;
    an_example.x=33;
    should be

    Code:
    class Example
    {
      public:
       Example (void) {}: 
       int x;
    };
    
    Example *an_example=new Example();
    an_example->x=33;

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Classes are the same as structs. Except classes default to private, whereas structs default to public.

  10. #10
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    --MrDoomMaster
    The kind of DooM that makes the MooD

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by Bubba
    Code:
    class Example
    {
    public:
          int x;
    };
    
    Example an_example;
    an_example.x=33;
    should be

    Code:
    class Example
    {
      public:
       Example (void) {}: 
       int x;
    };
    
    Example *an_example=new Example();
    an_example->x=33;
    Care to explain yourself?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    Care to explain yourself?
    Thank you! Man... I don't even know where this is going! lol... I just wanna know what the structure is doing in each action it takes.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    : Gosub Module1
    :
    : Module1:
    :    PRINT "This is a module"
    :    RETURN
    Also worth mentioning that in QB you never have to use GOSUB except in very special circumstances.

    And that is not a module: You have defined module1: to be a label, not a module. The entire file is the module.

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Bubba, you forgot 'delete an_example;'

    Anyways, when you have something like that, 'example' is essentially its own data type. Its a template, if you will.

    For example, you don't say:
    int = 1;
    You first create an instance of an integer, and then set it equal to 1.
    int x = 1;

    In the same way, 'example' is a type, and you need to create an instace of it:
    example an_example;
    ...or dynamically...
    example* an_example = new example;
    ... do some stuff, and free the memory ...
    delete an_example;

    (Note: the first method is recommended unless there is a reason you are dynamically allocating it, which I'm guessing you haven't gotten to yet.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  15. #15
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Classes/structs allow you to group data which has something in common. For example, here is the traditional Cat class:

    Code:
    class Cat
    {
    public:
          int Age;
          int Weight;
          void Meow();
          void Sleep();
    };
    You group all the things related to Cats in one object. But, what if you have two cats?

    Code:
    Cat Tintin;
    Cat Chelsea;
    (Those are the names of my cats)
    Those statements create two objects based on the type 'Cat'. Ie, both objects have Age and Weight, and they can both Meow() and Sleep(). You cannot do this:
    Code:
    Cat.Age=5;
    Because Cat is not an object, it is a type. Therefore, there is actually no variable in memory which can be assigned the value '5'.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Page 1 of 8 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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