Inheritance in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaheda begum
    New Member
    • Oct 2007
    • 12

    Inheritance in C++

    Do we have some provision in C similar to inheritance in C++???Thank you in advance.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by shaheda begum
    Do we have some provision in C similar to inheritance in C++???Thank you in advance.
    C is not OOP language in general(but with little more effort you can almost use it as if it is),but you should be able to inherit from the struct,because struct is nothing but a class where all members are public.

    Savage

    Comment

    • Andr3w
      New Member
      • Nov 2007
      • 42

      #3
      If I am not mistaken what he means is that you are allowed to do the following

      [code=c]
      struct a {
      int a, b;
      };

      struct b {
      struct a inheritLike;
      char b;
      };
      [/code]

      now later in your program or function you could write

      [code=c]
      struct b foo_struct;

      // more stuff

      foo_struct.inhe ritLike.a = 0;
      [/code]

      Hope this helped!

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by Andr3w
        If I am not mistaken what he means is that you are allowed to do the following

        [code=c]
        struct a {
        int a, b;
        };

        struct b {
        struct a inheritLike;
        char b;
        };
        [/code]

        now later in your program or function you could write

        [code=c]
        struct b foo_struct;

        // more stuff

        foo_struct.inhe ritLike.a = 0;
        [/code]

        Hope this helped!
        No need for that,he can do:
        [CODE="c"]
        struct a {
        int a, b;
        };

        struct b:a
        {
        int c;
        };[/CODE]

        Now struct b has both a and b plus its own c,and can access them as if they were part of b and not of a.
        [CODE="c"]
        b bInst;
        bInst.a=bInst.b =bInst.c;[/CODE]

        Savage

        Comment

        • Andr3w
          New Member
          • Nov 2007
          • 42

          #5
          If I try to compile the following just fails...

          [code=c]
          struct a {
          int a, b;
          };

          struct b:a
          {
          int c;
          };

          int main ()
          {
          return 0;
          }
          [/code]

          I put what you gave as code to a simple just main program but it fails to compile. Can you help? But one thing I don't understand is how can I use " : " in order to inherit because ansi C it doesn't allow me to use that. Also for the above post I thought you meant with the term "inheritanc e in C" what I wrote before but I was wrong, sorry :p.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by Andr3w
            If I try to compile the following just fails...

            [code=c]
            struct a {
            int a, b;
            };

            struct b:a
            {
            int c;
            };


            int main()
            {

            b bInst;
            bInst.a=bInst.b =bInst.c;


            return 0;
            }
            [/code]

            I put what you gave as code inside a main function but it fails to compile. Can you help? But one thing I don't understand is how can I use " : " in order to inherit because ansi C it doesn't allow me to use that. Also for the above post I thought you meant with the term "inheritanc e in C" what I wrote before but I was wrong, sorry :p.
            I tried TC,Borland C++ Builder 6(created c console app),and VC 2005 Pro.On all 3 this compiles.Can you tell me what you got as error?

            Savage

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by Savage
              I tried TC,Borland C++ Builder 6(created c console app),and VC 2005 Pro.On all 3 this compiles.Can you tell me what you got as error?
              Having one struct extend another is just a variation on one class extending another. You can't do that it C -- at least not ANSi C. If a compiler allows you, I bet it is regarding your code as C++.

              In any case, for inheritance to really leverage, you need polymorphism. C doesn't have this -- it's not an object-oriented language like C++, Java, etc... Why pretend it is?

              And maybe the real point, is why are you asking? What are you trying to do? what is your goal?

              Comment

              • Andr3w
                New Member
                • Nov 2007
                • 42

                #8
                Well the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile

                Comment

                • alijannaty52
                  New Member
                  • Dec 2007
                  • 17

                  #9
                  It is possible to write object oriented programs in plain C. This is often
                  uglier, and more dangerous than doing it in an object oriented langauge but
                  you can do it.Hope below link will help you indeed.

                  http://aspspider.info/magicalspell4u/?Quest=C-Oops

                  -Thanks
                  52

                  Comment

                  • Andr3w
                    New Member
                    • Nov 2007
                    • 42

                    #10
                    As for reading stuff you can also find this book (it's free):
                    http://www.cs.rit.edu/~ats/books/ooc.pdf

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Originally posted by Andr3w
                      Well the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile
                      Can't you just use .cpp files instead of .c files ??? That would compile your code as C++ and you could use inheritance.

                      Otherwise, you use an embedded struct member as has been suggested.

                      Comment

                      • Andr3w
                        New Member
                        • Nov 2007
                        • 42

                        #12
                        well, I suppose I could but then it would be a cpp program. Wouldn't it? The topic was if we could use inheritance in C in the first place :P

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          There is no inheritance in C.

                          Is there some reason you can't use C++??

                          Comment

                          • BigDaddyLH
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1216

                            #14
                            Originally posted by alijannaty52
                            It is possible to write object oriented programs in plain C. This is often
                            uglier, and more dangerous than doing it in an object oriented langauge but
                            you can do it.Hope below link will help you indeed.

                            http://aspspider.info/magicalspell4u/?Quest=C-Oops

                            -Thanks
                            52
                            It's also possible to write object oriented programs in COBOL or assembler. But why not use a language that supports what you are trying to do? You still haven't described your problem adequately. What is the context, what are you trying to do and why?

                            I still think the answer is to use C++ or Java. Now what was the question?

                            Comment

                            Working...