Inheritance and Polymorphism

This is a discussion on Inheritance and Polymorphism within the C++ Programming forums, part of the General Programming Boards category; Sample code of these oop words. Any replies would be appreciated. Thank's 1.Single inheritance 2.Multiple inheritance 3.Multi-level inheritance 4.Ad hoc ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    Thumbs up Inheritance and Polymorphism

    Sample code of these oop words. Any replies would be appreciated. Thank's

    1.Single inheritance
    2.Multiple inheritance
    3.Multi-level inheritance
    4.Ad hoc polymorphism
    5.Pure Polymorphism
    Last edited by bench386; 03-19-2004 at 01:01 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    1) A->B
    A B (2
    \ /
    C
    3) A->B->C
    4) & 5) difference between ad hoc and pure in this context, haven't the foggiest. Sorry.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903

    Re: Inheritance and Polymorphism

    1.Single inheritance
    -- example:
    Code:
    class myClass1
    {
    // stuff
    };
    
    class myClass2:public myClass1
    {
    // more stuff, but now can use stuff from myClass1 too
    };
    2.Multiple inheritance
    -- example:
    Code:
    class myClass1
    {
    // stuff
    };
    
    class myClass3
    {
    // stuff2
    };
    
    class myClass2:public myClass1, public myClass3
    {
    // more stuff, but now can use stuff from myClass1 and myClass3 too
    };
    3.Multi-level inheritance
    -- example:
    Code:
    class myClass1
    {
    // stuff
    };
    
    class myClass3:public myClass1
    {
    // can use stuff from myClass1
    };
    
    class myClass2:public myClass3
    {
    // more stuff, but now can use stuff from myClass1 and myClass3 too
    };
    4.Ad hoc polymorphism
    5.Pure Polymorphism

    polymorphism has to do with stuff like this:
    -- example:
    Code:
    class myClass1
    {
         virtual void myFunc()=0;   // Means it must be implemented by the inheriting class
    };
    
    class myClass2:public myClass1
    {
         void myFunc();
    };
    myClass1* shell=new myClass2();
    
    shell->myFunc();   // Calls myFunc from myClass2
    We create a myClass1 pointer, but instantiate it with a myClass2 type, This is really nice because we could swap in and out different "modules" just by changing one pointer!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. polymorphism and inheritance
    By stewie griffin in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2009, 02:45 AM
  2. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 09:47 AM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. Polymorphism, inheritance and containers
    By musicalhamster in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2007, 10:23 AM
  5. inheritance and polymorphism uses...
    By tetra in forum C++ Programming
    Replies: 5
    Last Post: 05-06-2003, 06:30 PM

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