Why inheritance is your daddy!

This is a discussion on Why inheritance is your daddy! within the A Brief History of Cprogramming.com forums, part of the Community Boards category; This post is of no importance other than for me to rant (joyfully) because I had ignored something simple, yet ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Why inheritance is your daddy!

    This post is of no importance other than for me to rant (joyfully) because I had ignored something simple, yet powerful, that proved to be VERY helpful to me.

    "Who needs to know that inheritance stuff?", I mused back then. Man, was I missing the boat.

    Long-story-short, my game has 2 groups (robots with computer-controlled AI and Soldiers controlled by the human player). They have a lot of identical attributes/functions with only a handful of different ones (AI stuff for the bots and input getting routines for the soldiers).

    I was midway through retyping the same code in the creation of my soldier *.h/*.cpp files when I thought there has to be a better way of doing this! About 15 minutes of research later and I was redesigning my soldiers/bots using a base class (Trooper) with 2 derived classes (CPow and CBot, respectively).

    Any of you guys/gals ever neglect learning a particular part of the language because you figured you wouldn't need it (at least for the then-current project)?

    Now I've got to go back and rewrite my CTextBox, CLabel, CCombobox and CCommandButton.

  2. #2
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Any of you guys/gals ever neglect learning a particular part of the language because you figured you wouldn't need it (at least for the then-current project)?
    yeah, pointers and classes. i simply don't understand where these two things can come into play. i would love for somebody to show me otherwise. . . hint hint

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    pointers are important in organizing information. they can be used to represent a linked list, a tree, a map, etc. they free you of the limits, present in other languages, where variable names and physical data are forever bound. through using pointers you can change your program's whole enviroment simply by replacing one set of data with another.

    classes are meant to organize data. classes hold data and do stuff. you can treat each object of a class like its real world counterpart. a class masks most of the details you might find and gives you a convienent interface to work with.

    say you had a class USDollar.
    Code:
    USDollar x = (USDollar)y + (USDollar)z;
    that is reasonably simple. but what about:
    Code:
    Euro x = (USDollar)y + (JapaneseYen)z;
    things get a little tricky, but with classes this example seems to make sense. would you prefer to work with this interface, or would you prefer to do everything yourself?

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    "Any of you guys/gals ever neglect learning a particular part of the language because you figured you wouldn't need it (at least for the then-current project)? "

    Inheritance . Its just never seen useful in any of my projects, mainly since most of the classes are very different.

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by TechWins


    yeah, pointers and classes. i simply don't understand where these two things can come into play. i would love for somebody to show me otherwise. . . hint hint
    i hope you sarcasm.
    hello, internet!

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    > Its just never seen useful in any of my projects, mainly since most of the classes are very different.

    That's because you design your projects without inheritence.

    >"Who needs to know that inheritance stuff?", I mused back then. Man, was I missing the boat.

    Yes, me too. There was a time when I thought code re-use was the act of cutting & pasting chucks of code.

  7. #7
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Davros
    [BThere was a time when I thought code re-use was the act of cutting & pasting chucks of code. [/B]
    Yep. I remember thinking that before! lol!

    By the way, with a username like Davros, you wouldn't happen to be a Doctor Who fan would you?

  8. #8
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    i hope you sarcasm.
    no, i'm being serious. i never use either one of them, because i couldn't ever think of where i might need to use them. i've always gotten by without them. i understand that they have a huge purpose, but i couldn't ever find that purpose. however, ygfperson, made it a little bit more to clear me where i could use them. thanks for that, ygfperson.

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Pointers are invaluable, just try building a dynamic Stack class!! I'm sure it can be done without pointers but its so much simpler with them.
    Couldn't think of anything interesting, cool or funny - sorry.

  10. #10
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>Pointers are invaluable, just try building a dynamic Stack class!!

    Just try building anything!

    Once upon a time (what seems like a really really long time ago) I couldnt see a reason to use pointers. Now I really cant imagine what you could do without them. It would be highly frustrating for me to build anything of any importance without using pointers. Pointers are [in my experience] a key element of a flexable, durable, application.

    Classes I could probably get by without technically, but it would bug the hell out of me. It'd be like getting by without any data types except long int. Simple yet needlessly frustrating. Speaking of data types; You probably use pointers all the time without really thinking about it. char * and all.

    I strongly suggest you learn about pointers and classes, TechWins. They'll be the gateway to a whole new level of programming.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Pointers are fascinating creatures. Some of their interesting features are:

    point-to-point copying: while( p != q ) (*p)++ = *q;
    indexes of functons: fPtr = condition ? f1 : f2;
    offset calculation: int offset = pCurr - pOrg;
    testing for ownership (doesn't always work!): while(Ptr!=NULL)
    pointers to pointers: int **far = &ptr and int **far = &array_of_pointers[0];


    classes are just structs with methods attached. From within, they call member variable by name and do not pass the structure pointer of the object into these functions but rather the object calls things internally with the "this" pointer as in:

    class foo{ public:

    int value;

    int ReturnValue()
    {
    return value; //...same as: return this->value
    }
    };


    compared with C's:

    typedef struct foo{ public:

    int value;
    };

    int FooReturnValue( foo *external)
    {
    return external->value;
    }


    so:
    int return_value = FooReturnValue( &object );

    becomes:
    int return_value = object.ReturnValue();


    Some overlooked techniques such as operator overloading can truly give joy to a project, take:

    class Needs { public:

    int* connection;
    int value;

    void operator << ( Needs has ) {

    this->connection = has->connection;

    this->value += has->value;
    }
    };



    int main() {

    int a = 5;

    Needs
    intoThis, copyThat;

    copyThat.connection = &a;

    copyThat.value = a;

    intoThis << copyThat;


    return 0;
    };






    here's a fun class to start with:



    Code:
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>  2002 Software Written By Precision Software/Sebastian McKelvy <<<<<<<//
    //<<<<<<<<<<<<<<<<<<<<<<<<  This is free software  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    
    
    class Notifiable {
    
    private:
    
    List <Notifiable*> subscribers;
    
    public:
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    void Attach( Notifiable *new_subscriber ){ subscribers.Push(new_subscriber); }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
    void Detach( Notifiable *thisMember )
     {
      List <Notifiable*> *reference = subscribers.Find(thisMember);
    
    
      if(reference != NULL) subscribers.Remove(reference);
     }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    virtual void OnNotify(){};
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    virtual void OnNotify(void *optional_parameter){};
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
    void Notify(void *optional_parameter = NULL)
     {
      if( subscribers.Count() == 0 ) return;
    
    
      List <Notifiable*> *invoke = &subscribers;
    
    
      while(invoke = invoke->next)
    
        if(optional_parameter != NULL)
    
        invoke->data->OnNotify( optional_parameter );
    
        else
    
        invoke->data->OnNotify();
     }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
    };
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>  2002 Software Written By Precision Software/Sebastian McKelvy <<<<<<<//
    //<<<<<<<<<<<<<<<<<<<<<<<<  This is free software  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    
    
    
    
    
    
    
    
    
    
    class Child: pubilc Notifiable { public:
    
    void OnNotify()
     { 
       cout << "generic response...\n"; 
       getch(); 
       Notify();
     }
    
    void OnNotify(void*message)
     { 
      cout << "custom response: " << (charr*)message <<"...\n"; 
      getch(); 
      Notify();
     }
    
    
    };
    
    
    
    class Parent: pubilc Notifiable { public:
    
    int count;
    char response[100];
    
    Parent():count(0){};
    
    void OnNotify(){ 
    sprintf(response, "%i", ++count); 
    Notify(response); 
    }
    
    };
    
    
    
    
    
    
    
    
    
    
    
    int main(){
    
    Parent parent;
    Child child;
    Child cousin;
    
    parent.Attach(&child);
    parent.Attach(&cousin);
    
    cousin.Attach(&child);
    
    //...for fun...uncomment:
    
    //cousin.Attach(&parent);
    
    
    parent.Notify((char*)"\"...Calling...\"");
    
    
    getch();
    
    return 0;
    }

    Have fun!
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  12. #12
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    class foo{ public:

    int value;

    int ReturnValue()
    {
    return value; //...same as: return this->value
    }
    };
    However, there is no reason the same thing cant be done with a struct. I occasionally use structs with member functions. As a matter of principal I use them for data storage but it can be handy to have your data automatically assigned default values when the struct is initialised (a constructor). Likewise I occasionally use a destructor as a safety to ensure that any memory the struct may have allocated during its life span is deleted when the struct leaves scope. Other instances of using additional member functions (besides constructor/destructor) come to mind but you get the picture.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Yes but most of the time, when someone says they no little of classes it means they can't use them outside of the older calling traditions. And struct's and classes are one in the same in C++, except for the opposite private/public defaults for each.

    One thing C nevered offered was more flexible assignment, like:

    int values[100];
    vector vValues;

    //...code...

    if(vVector == values)
    //...== overloaded...

    C++ makes objects reusable and meaningful.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  14. #14
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Ah, overloading; Another concept I ignored for so long and have become so attached to now that I understand it fully.

    I really cant understand why people are so attached to C over C++. C++ brings so many versatile concepts that its just insane to not take advantage of them.

    >>And struct's and classes are one in the same in C++, except for the opposite private/public defaults for each.

    Though any well written class could be written as a struct without a single change (besides the obvious change of keywords 'class' and 'struct' ). I dont believe that the defaults for either should be assumed. I always declare public/private explicitly in each instance.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  15. #15
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> I really cant understand why people are so attached to C over C++

    I must admit that I was attached to C until i discovered the wonders of C++. I'd say that if you are really good in C programming, then why not give C++ a chance and see what all the hype's about. And it's a fact; if you know C and start learning C++, you'll learn the language extremely fast and in no time you'll be bloody good at it.(Not that you have to know C to become proficient at C++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 09:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

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