Results 1 to 11 of 11

Thread: can somebody help me not suck at c++ plzkthnx

  1. #1
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default can somebody help me not suck at c++ plzkthnx

    wtf are constructors and destuctors for?! omg im so confused!!! is it really neccesary to have

    Nutz()
    ~Nutz()

    if it doesnt do anything?! can someone explain this to me in lamemans terms? i be weetahded. i will lub you foreber
    Oh gods, why? ಥ_ಥ


  2. #2
    Magixion's Avatar
    Join Date
    Jul 2008
    Location
    The World of Ruin! Noooo!
    Posts
    371

    Default

    Constructors and destructors are basically exactly what their name implies. Constructors instantiate and create the object while destructors destroy the object. You have to remember that any variant of C is an object oriented language, so you have to have a certain mindset for it. Really, once you get used to it, these will become second nature and very, very easy.
    Last edited by Magixion; 08-21-2008 at 03:50 PM.

    "Did I become a hero?"

    Project: N00b

  3. #3
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    And don't beat yourself up too much over C++ being hard. It's one of the most complex OO languages to learn.
    Figaro Castle

  4. #4
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    but could you access the object later on or do you have to recreate it?
    Oh gods, why? ಥ_ಥ


  5. #5
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    When you declare an object using the constructor, you now have a variable name as a pointer to the object data in in memory. When you call delete() on an object, this calls the destructor. The effect of this is to remove the data pointed to by the variable name, invalidating the pointer.

    In plain English, you should have one call to "delete" for every "new" used in object creation, and after you call delete, you can't access that object again.


    In this diagram, each box is some memory block, containing some data. The variable name in the first box points to the object in the second box. The 0x0001 is just an arbitrary memory address.
    <pre style="line-height: 8px;">joe = new Person("Joe", "Bloggs");
    causes:
    ________ <u>0x0001 </u>
    | joe: | | firstname: "Joe" | Memory taken off of the "safe to use" pile and
    | 0x0001 | ==> | surname: "Bloggs" | Joe's data stored in it.
    |________| |____________________|



    delete joe;
    causes:
    ________ <u>0x0001 </u>
    | joe: | | Previous memory | Memory gets returned to the "safe to use" pile
    | 0x0001 | ==> | of joe but memory | and other objects may now occupy it. For this reason,
    |________| | is now available | referring to an object after it's been deleted could
    | to other objects | mean you're referring to some other object unknowingly
    |____________________| and is therefore unsafe.
    </pre>

  6. #6
    it's not fun, don't do it Moon Rabbits's Avatar
    Join Date
    Mar 2005
    Posts
    5,582

    Default

    o_O's explanation is great. I'd also like to point out that objects do not have to be instantiated as pointers (although this is the most useful way to do so). When not using pointers, the constructor is called upon the implementation of an object, ie:

    class SomeClass {
    public:
    SomeClass() {/*do stuff */ };
    ~SomeClass() {/*free stuff*/};
    };

    SomeClass someclassobject; // constructor would be called

    Once your program exits, the deconstructor will be called. Deconstructors are useful for freeing memory. For instance, say you have an object that represents the player and stores a bitmap full of animations for the player. The constructor could load the bitmap and the deconstructor could free the memory used by the bitmap.

  7. #7
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    ok. a week ago that wouldve confused me cuz i just got passed the part in the book with pointers and free space. im starting to understand constructors now thanks. but what is getting me now is overloading operators XD
    Oh gods, why? ಥ_ಥ


  8. #8
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Overloading is thankfully easier than understanding blocks of memory and pointers.

    Consider this scenario:

    You have a class that represents a vector, and you need to come up with a way to add them together using the "+" operator. You'd normally store a vector as an array of numbers, but if you try to add two arrays together, you'd get a compile-time error:
    PHP Code:
    int arr1[3] = { 12};
    int arr2[3] = { 45};

    cout << arr1 arr2// Bad 
    However, we can overload the + operator to execute code that actually add the two vectors together:
    PHP Code:
    // Note: Calling the class "Vect" since there are already Vector libraries for C++ called "Vector" :p
    class Vect
    {
        
    int vector[];    // This array will hold the values in the vector
        
    int size;
        
    Vect::Vect(int numElementsint *values)    // Constructor to assign vector values
        
    {
            
    vector values;
            
    size numElements;
        }
        
        
    Vect Vect::operator+ (Vect vector1Vect vector2)    // Overloaded + operator
        
    {
            
    Vect added(vector1.size);    // Creates a new vector to contain added values
            
    for (int i 0vector1.size && vector2.sizei++) {
                
    added[i] = vector1[i] + vector2[i];
            }
            return 
    added;
        }
    }

    // Then we can call this code:
    Vect arr1(3, {123});
    Vect arr2(3, { 45});

    cout << arr1 arr2// Good 
    In plain English, overloading operators allow to to make two objects do whatever you like when you use some operator on them, e.g. obj1 + obj2; obj1 * obj2, etc.
    P.S. I haven't tested that code or anything, so don't expect it to work first time (there's bound to be something funky going on with pointers and references there).

  9. #9
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    hmmmm...... so you are basically making something up to manipulate the data? meh..... im not that smart. im probably gonna have issues with it. my main weakness has always been arrays and such. new concepts + arrays = x_x
    Oh gods, why? ಥ_ಥ


  10. #10
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Basically it's defining the way two objects behave when an operator is used on them. You can create an instance of an object such as a string, for example - what should happen when somebody says:
    PHP Code:
    string s("lol" " internets"); 
    In the rules of English, there's no default action that you should take when somebody applies addition to a string, so in C++ you can overload the operator and make the two strings actually do something useful when you add them. Like joining them (this is actually what C++ already does, but it's an example of how operators are overloaded. ).
    PHP Code:
    string string::operator+ (string firststring second)
    {
        
    // Some code here to join both strings together and return them

    Similarly, what should happen when you try to divide two strings? C++ doesn't define an action for string division, so you could choose whatever behaviour you like. You could return the lengths of the strings divided; you could return the first half of the first string matched up with the second half of the second string and vice versa; you could even do something like this:
    PHP Code:
    string string::operator/ (string firststring second)
    {
        return 
    "That doesn't even make sense!";

    Then, if someone tried to divide strings:
    PHP Code:
    string s1("lol");
    string s2("internets");
    string s3 s1/s2;
    // The string "s3" now contains the string "That doesn't even make sense 

  11. #11
    diafnaoplzkthnxbai NeoTifa's Avatar
    Join Date
    Jun 2005
    Location
    in psy's panties <3
    Posts
    3,411

    Default

    oic!!!! XD thnx!
    Oh gods, why? ಥ_ಥ


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •