what is Difference between structure and class in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinendrashankar
    New Member
    • Nov 2007
    • 10

    what is Difference between structure and class in C++

    Hi All,

    Can any one have any idea about this issue

    what is Difference between structure and class in C++
    Other then by default structure have public scope and class have private scope For data member and member function .If there is no other difference between Class & structure then why we use 2 key world in C++ we can use either class or structure in C++ ?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by jinendrashankar
    Hi All,

    Can any one have any idea about this issue

    what is Difference between structure and class in C++
    Other then by default structure have public scope and class have private scope For data member and member function .If there is no other difference between Class & structure then why we use 2 key world in C++ we can use either class or structure in C++ ?
    1) Strutures don't provide something like data hiding which is provided by the classes.

    2) Structures contain only data while class bind both data and member functions.

    3) Structure dosen't support the polymorphism, inheritance and initilization.

    4) In a Structure we can't initilize the value to the variable but in class variable we can assign the values.

    And you told is already one of the differences!

    Regards

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Originally posted by zodilla58
      1) Strutures don't provide something like data hiding which is provided by the classes.

      2) Structures contain only data while class bind both data and member functions.

      3) Structure dosen't support the polymorphism, inheritance and initilization.

      4) In a Structure we can't initilize the value to the variable but in class variable we can assign the values.

      And you told is already one of the differences!

      Regards

      Zodilla ,
      I dont agree with what you have told.
      Structures can have constructors, destructor, VTable, inheritance, overriding, overloading etc. everything a class can have.

      Thanks
      Raghuram

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by gpraghuram
        Zodilla ,
        I dont agree with what you have told.
        Structures can have constructors, destructor, VTable, inheritance, overriding, overloading etc. everything a class can have.

        Thanks
        Raghuram
        Raghuram,

        I don't have ever encountered with such a structure!! Can u give an example to support this? It will clearify my doubt regarding this.

        Okie.. I found examples of constructor and overloading! But couldn't found inheritance example!

        Thanks
        Last edited by Meetee; Dec 6 '07, 07:01 AM. Reason: Okie..found example

        Comment

        • scruggsy
          New Member
          • Mar 2007
          • 147

          #5
          Originally posted by zodilla58
          Raghuram,

          I don't have ever encountered with such a structure!! Can u give an example to support this? It will clearify my doubt regarding this.

          Okie.. I found examples of constructor and overloading! But couldn't found inheritance example!

          Thanks
          Try here.
          Decent explanation of the only difference between structs and classes and the reason for having both keywords,

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            Yup, no difference but the default access type. Yes, it is idiomatic to use structs for arbitrary data types, and classes for the whole, well, class thing. But C++ actually doesn't really differentiate between the two. Crazy, but true.

            EDIT: You can express your disbelief as much as you want. If you find it so absurd, write some code and check it for yourself. C++ structs are not the same as C structs. Just because you use them idiomatically as C structs does not mean a thing.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Hey, just check it out for yourself.

              Write a class with constructors, destructors and member funcitons. Once it's compiled an working go in there and change the class to struct and rec-compile. You will find that everything works as before.

              Why is this??

              Here's why:

              1) The only way to group data variables together in C is to use a struct.
              2) C++ is a replacement for C
              3) The only way to group data variables together in C++ is to use a struct.
              4) Object-oriented features (which you don't have to use) were added to C++
              5) That involved a "class" with "public" and "private" members. This was due to the object-technology folks who use a "class of things" and an "instance of a class".
              6) The class in C++ was implemented as a struct.
              7) To make C++ look like an object-oriented lamguage, the "class" keyword was added as an alternate declaration of a struct.
              7) The only difference is that the default access to a class is private whereas the default access for a struct is public.

              Comment

              • jac666
                New Member
                • Dec 2007
                • 1

                #8
                Originally posted by weaknessforcats
                (...)
                7) The only difference is that the default access to a class is private whereas the default access for a struct is public.
                C++ "class" = "struct" except:
                1. The default access to the members of a class is private whereas the default access for a struct is public
                2. The default inheritance is adequate to the default access.

                struct A : B {}; // the same as: class A : public B {};
                class A : B {}; // the same as: class A : private B {};

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by jac666
                  2. The default inheritance is adequate to the default access.
                  This is part of the default access: struct is public, class is private.

                  Comment

                  • gpraghuram
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1275

                    #10
                    Originally posted by weaknessforcats
                    Hey, just check it out for yourself.

                    Write a class with constructors, destructors and member funcitons. Once it's compiled an working go in there and change the class to struct and rec-compile. You will find that everything works as before.

                    Why is this??

                    Here's why:

                    1) The only way to group data variables together in C is to use a struct.
                    2) C++ is a replacement for C
                    3) The only way to group data variables together in C++ is to use a struct.
                    4) Object-oriented features (which you don't have to use) were added to C++
                    5) That involved a "class" with "public" and "private" members. This was due to the object-technology folks who use a "class of things" and an "instance of a class".
                    6) The class in C++ was implemented as a struct.
                    7) To make C++ look like an object-oriented lamguage, the "class" keyword was added as an alternate declaration of a struct.
                    7) The only difference is that the default access to a class is private whereas the default access for a struct is public.

                    Thanks for your explanation..
                    Raghuram

                    Comment

                    Working...