Thread: Simple inheritance example??

  1. #1
    Unregistered
    Guest

    Unhappy Simple inheritance example??

    Hello,

    I want to learn inheritance for a game that I am trying to write, but can't find any good examples. Could someone write 2 simple classes where one is derived from the other?? I'd really appreciate it! Also, if you could thoroughly explain what you're doing it'd really help me. Thanks a lot!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    //base class 
    
    class Human
    {
        protected:
            int life;
        public:
            virtual void Die() = 0;
            virtual void Damage( int dmg ) = 0;
    };
    
    //derived class 1
    
    class Civilian : public Human
    {
        public:
    
        Civilian()
        {
            life = 10;
        }
        void Die()
        {
            printf( "...dead" );
        }
        void Damage( int dmg )
        {
            life = life - dmg;
            if( life < 0 ) Die();
        }
    };
    
    //derived class 2
    
    class Soldier : public Human
    {
        public:
    
        Soldier()
        {
              life = 15;
        }
        void Die()
        {
            printf( "...throwing last handgrenade... dead" );
        }
        void Damage( int dmg )
        {
            life = life - ( dmg - 1 ); // 1 point armor
            if( life < 0 ) Die();
        }
    };
    
    void Hurt( Human& person )
    {
        person.Damage( 5 );
    }
    
    int main()
    {
        Civilian a;
        Soldier b;
    
        Hurt( a );
        Hurt( a );
        Hurt( a );
        Hurt( a );
    
        Hurt( b );
        Hurt( b );
        Hurt( b );
        Hurt( b );
        Hurt( b );
        Hurt( b );
    
        return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Great example. But if you put Hurt as a virtual function in your base class you could either except it or change Hurt's power rather than adjusting it in Damage. Then it would be completely polymorphic.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I could just call Damage( 5 ) on a and b in main, but that would
    defeat the purpose of showing that you can now have a generic
    Human and don't care what type it is...
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    That was cool


    Marky_Mark

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  3. simple inheritance
    By waxydock in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2005, 11:08 AM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM