Structures? *brain melts*

This is a discussion on Structures? *brain melts* within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Bubba Code: : Gosub Module1 : : Module1: : PRINT "This is a module" : RETURN Also ...

  1. #16
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by Bubba
    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.

    sorry this isn't helping.

    But for what it's worth, I know how to program modules in QB, for god's sake, it's in the damn textbook... MODULES... and it's using the same structure as I am...

    ANYWAY... BACK TO C++ PLEASE??!

    No one let me bring up QB again!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    In order to create an instance of a class you must provide a constructor. The constructor is called at creation time. The way in which you instantiated the class is incorrect and will generate an error from the compiler. Classes should be instantiated with new - they are like structs but they cannot be thought of exactly in the same terms. For information on why this is...consult a C++ book.

    But for this discussion structs and classes have little or nothing to do with the concept of a module. So to get back to the point a module is just one source code file in a project whether it be in PASCAL, assembly, BASIC, or C/C++.

  3. #18
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    But for this discussion structs and classes have little or nothing to do with the concept of a module. So to get back to the point a module is just one source code file in a project whether it be in PASCAL, assembly, BASIC, or C/C++.
    Actually, I think the DoomMaster was more concerned with the struct code that he had.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #19
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    But for what it's worth, I know how to program modules in QB, for god's sake, it's in the damn textbook... MODULES... and it's using the same structure as I am...
    Then I would suggest tossing the textbook in the trash because its wrong.

    This is not a module in C:

    Code:
    struct MyStruct
    {
       int foobar;
    };

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Actually, I think the DoomMaster was more concerned with the struct code that he had.
    But he mixed two terms that have nothing to do with each other.
    Module and struct.

    Both statements about both languages were inaccurate. Thinking of a struct in terms of a module will have you taking Advil in no time. They are not the same nor even close to the same thing.

  6. #21
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    In order to create an instance of a class you must provide a constructor. The constructor is called at creation time. The way in which you instantiated the class is incorrect and will generate an error from the compiler.
    I have often omitted a constructor. I have NEVER had an error.

    Classes should be instantiated with new - they are like structs but they cannot be thought of exactly in the same terms.
    Every tutorial I used never did it with 'new'. Nothing ever went wrong. Could you explain a bit further, as I do not have a C++ book.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #22
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Bubba
    Classes should be instantiated with new...
    There's no more reason to dynamically allocate instances of a structure than there is a primitive type.
    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.

  8. #23
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    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'.
    Hmm... so, Cat isn't a user-defined name? same with Example?

    hmmmmmmmm.... I get mad when I can't figure something out. Is it just me being stupid, or is this really hard to figure out?
    --MrDoomMaster
    The kind of DooM that makes the MooD

  9. #24
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Hmm... so, Cat isn't a user-defined name? same with Example?
    I'm not sure what you mean by user-defined name.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #25
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Additionally, with constructors:

    If you provide none, the compiler will generate for you a default constructor. Unless you explicitly declare them away, it will also generate a shallow copy constructor and assignment operator. When creating an object from its default constructor, the parens can be omitted. That is, the following statements are equivalent:

    MyClass a;
    MyClass a();

    Same with:

    MyClass* a = new MyClass;
    MyClass* a = new MyClass();
    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.

  11. #26
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Well if you do not provide a constructor then when your object is created the compiler will provide one. However, this is not a good practice. Let's say for instance you have a pointer in your class. If you do not provide a constructor and init the pointer to 0 you have a NULL pointer. Accessing it later w/o first initializing it will crash the system. IMO all classes should have a constructor and I'm not sure what the ISO C++ standard says about it.

    New is basically malloc(sizeof(object)). New is not C it is C++ which is why your books don't use it. But classes are C++ so I can't understand why a C book would include classes in it. You can instantiate classes w/o new but again I don't recommend doing this as it will only serve to confuse you. For that matter you can put constructors in structs, but again we are getting way off topic here.


    Where are you guys getting these books?

  12. #27
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by MrDoomMaster
    Hmm... so, Cat isn't a user-defined name? same with Example?

    hmmmmmmmm.... I get mad when I can't figure something out. Is it just me being stupid, or is this really hard to figure out?
    It is a user defined type. A type just like int.

    Tintin is an instance of Cat. That is, the following are analogous:

    Cat Tintin; // Create a new Cat named Tintin
    int x; // Create a new int named x
    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.

  13. #28
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You can instantiate classes w/o new but again I don't recommend doing this as it will only serve to confuse you
    Believe me, the only thing confusing me is YOU. I've been using classes for a couple years now, and I use them both ways, with new, and without. I've never been confused.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #29
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by bennyandthejets
    I'm not sure what you mean by user-defined name.
    well, I mean, for example... you can't initialize a variable called INT, because that is a reserved word... right?

    So, INT is not user-defined... yet, a variable name like int123 would be user defined, because the programmer made up the name himself... got it?

    Now, I know we're not talking about variables in the question of structures, but I was just using variables to explain to you what I mean't by user-defined. I'm sorry if my terminology is off, but I hope you catch my drift!

    I'm a work in progress, I understand how hard it is to be patient with people that just take FOREVER to understand something that you see so simple, but I'm really trying as hard as I can. Remember, we all had to learn this at one point. I thank everyone for your help, including you Bubba, even though I have no idea what you're saying most of the time lol. Thanks though It's the thought that counts!


    How about I just tell you how I understand structures so far, and you can fill in the gaps?

    Okay, explaining this as simple as I can... A structure, to me, is a scope enclosed with curly braces that contains variable initializations, for example:

    Code:
    struct myvariables
    {
        int variable1
        int variable2
    }
    and there has to be some way to call on one of these variables that are contained inside of the above "scope" right? So, I figure you would put the name of the structure, then a period, and the name of the variable inside of the structure that you want to change, like so:

    myvariables.variable1=3

    To me, this would say "Look inside of structure 'myvariables' and find 'variable1' then set it to 3!"

    See how I see things now? That's why the "example an_example" throws me off! It doesn't fit in with what I understand! Anyhow, I hope this helps!

    Thanks for everyone's patience!
    --MrDoomMaster
    The kind of DooM that makes the MooD

  15. #30
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Bubba
    Well if you do not provide a constructor then when your object is created the compiler will provide one. However, this is not a good practice. Let's say for instance you have a pointer in your class. If you do not provide a constructor and init the pointer to 0 you have a NULL pointer. Accessing it later w/o first initializing it will crash the system. IMO all classes should have a constructor and I'm not sure what the ISO C++ standard says about it.

    New is basically malloc(sizeof(object)). New is not C it is C++ which is why your books don't use it. But classes are C++ so I can't understand why a C book would include classes in it. You can instantiate classes w/o new but again I don't recommend doing this as it will only serve to confuse you. For that matter you can put constructors in structs, but again we are getting way off topic here.


    Where are you guys getting these books?
    Yes, there are reasons to provide constructors a lot of the time. There is not always a reason, however. Additionally, if you want to get rid of the copy semantics, and default constructor, you must declare them away:

    Code:
    class X
    {
    public:
      // Whatever constructors you want.
    private:
      // Declare away!
      X();
      X(const X&);
      X& operator=(const X&);
    };
    Using dynamic allocation (new) will probably be more confusing at this state. Not the 'new' operator itself, but the fact that you are responsible for cleaning up (where appropriate, and not before) any memory you allocate.

    Just go with the example, and create it on the stack (i.e. without new).
    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.

Page 2 of 8 FirstFirst 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