difference between a structure and a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shea Martin

    difference between a structure and a class

    I have been programming in C++ for over 4 years. I *think* I knew that
    a struct could have a constructor but I decided to dig into it a little
    more today, and found that there is very little difference between a
    struct and a class in C++. Both have inheritance, access modifiers,
    etc. The only diff I see is the default access level is public vs.
    private.



    I have always just used structs as a minimal class, as that is the way
    they were used in textbooks.



    Do structs have all these features in pure C? If not then why did c++
    change them, when they were adding the Class type? Are there more
    differences than those I have listed? Performance differences? I have
    always just used structs as a minimal class, as that is the way they
    were used in textbooks.



    Thoughts, Ideas?



    ~S


  • Mike Wahler

    #2
    Re: difference between a structure and a class


    "Shea Martin" <sam_squirrelnu tz@hotmail.com> wrote in message
    news:h1%ne.4139 4$Ph4.937036@ur sa-nb00s0.nbnet.nb .ca...[color=blue]
    >I have been programming in C++ for over 4 years. I *think* I knew that
    > a struct could have a constructor but I decided to dig into it a little
    > more today, and found that there is very little difference between a
    > struct and a class in C++. Both have inheritance, access modifiers,
    > etc. The only diff I see is the default access level is public vs.
    > private.[/color]

    That is indeed the only difference. Note that also included
    in 'access' is the default inheritance: class has 'private',
    and struct has 'public'. That's why you'll typicall see
    derived classes declared as:

    class B : public A
    [color=blue]
    > I have always just used structs as a minimal class, as that is the way
    > they were used in textbooks.[/color]

    Anything that can be done with a class can also be
    done with a struct.
    [color=blue]
    >
    >
    >
    > Do structs have all these features in pure C?[/color]

    No.

    [color=blue]
    >If not then why did c++
    > change them,[/color]

    Because C++ is not C.
    [color=blue]
    > when they were adding the Class type?[/color]

    'class' is not a type. 'class' (and 'struct') is a keyword
    the programmer can use to create custom (aggregate) types.

    [color=blue]
    >Are there more
    > differences than those I have listed?[/color]

    No.
    [color=blue]
    >Performance differences?[/color]

    That's an implementation detail not addresses by the
    language. See "Quality of Implementation" .
    [color=blue]
    > I have
    > always just used structs as a minimal class, as that is the way they
    > were used in textbooks.[/color]

    Objects created from a 'struct' definition are no
    less than those created from a 'class' definition,
    so 'minimal' does not apply.
    [color=blue]
    > Thoughts, Ideas?[/color]

    Since the 'mechanics' are the same, I suggest using
    the different keywords 'class' and 'struct' as
    indicators of your intentions: I.e. use 'class'
    when you're creating abstract types with special
    functionality (e.g. polymorphism), use 'struct'
    if simply grouping related simple data items (e.g.
    a pair of integer objects). But ultimately, the
    choice is yours (or often that of your employer/
    client).


    -Mike


    Comment

    • John Carson

      #3
      Re: difference between a structure and a class

      "Shea Martin" <sam_squirrelnu tz@hotmail.com> wrote in message
      news:h1%ne.4139 4$Ph4.937036@ur sa-nb00s0.nbnet.nb .ca[color=blue]
      > I have been programming in C++ for over 4 years. I *think* I knew
      > that a struct could have a constructor but I decided to dig into it a
      > little more today, and found that there is very little difference
      > between a struct and a class in C++. Both have inheritance, access
      > modifiers, etc. The only diff I see is the default access level is
      > public vs. private.
      >
      > I have always just used structs as a minimal class, as that is the way
      > they were used in textbooks.
      >
      > Do structs have all these features in pure C?[/color]

      No. They don't have any of those features. Structs in C are just containers
      for data types.
      [color=blue]
      > If not then why did c++ change them, when they were adding the Class type?[/color]

      I suppose you would have to ask Stroustrup (or read his books), but you can
      use structs exactly as they are used in C or you can use them with the added
      features. Your choice. Isn't that better that only being able to use them as
      in C?
      [color=blue]
      > Are there more
      > differences than those I have listed? Performance differences? I have
      > always just used structs as a minimal class, as that is the way they
      > were used in textbooks.[/color]

      Default inheritance, in addition to access, is public with structs. I think
      that is about all the differences, but maybe there is one more.


      --
      John Carson

      Comment

      • Jordan DeLong

        #4
        Re: difference between a structure and a class

        On Fri, 03 Jun 2005 16:03:44 +0000, Mike Wahler wrote:[color=blue]
        > "Shea Martin" <sam_squirrelnu tz@hotmail.com> wrote in message
        > news:h1%ne.4139 4$Ph4.937036@ur sa-nb00s0.nbnet.nb .ca...[color=green]
        >>I have been programming in C++ for over 4 years. I *think* I knew that
        >> a struct could have a constructor but I decided to dig into it a little
        >> more today, and found that there is very little difference between a
        >> struct and a class in C++. Both have inheritance, access modifiers,
        >> etc. The only diff I see is the default access level is public vs.
        >> private.[/color]
        >
        > That is indeed the only difference. Note that also included
        > in 'access' is the default inheritance: class has 'private',
        > and struct has 'public'. That's why you'll typicall see
        > derived classes declared as:
        >
        > class B : public A[/color]
        [...][color=blue]
        > Since the 'mechanics' are the same, I suggest using
        > the different keywords 'class' and 'struct' as
        > indicators of your intentions:[/color]
        [...]

        Or better yet, to avoid putting tons of `public' in your code, you can
        just use `struct' instead when you declare classes. Many (most?)
        people seem to put public data members/functions at the top of their
        class (and private at the bottom); the default access specifiers for
        `class' are inconvenient for this style of layout (both for reading and
        for writing).

        class B : public A, public N, public Q<B> {
        public:
        void f();
        };

        vs.

        struct B : A, N, Q<B> {
        void f();
        };

        --
        Jordan DeLong
        fracture@allusi on.net

        Comment

        • Mike Wahler

          #5
          Re: difference between a structure and a class


          "John Carson" <jcarson_n_o_sp _am_@netspace.n et.au> wrote in message
          news:d7pv7h$rde $1@otis.netspac e.net.au...
          [color=blue]
          > Default inheritance, in addition to access, is public with structs. I
          > think that is about all the differences, but maybe there is one more.[/color]

          Yup -- different spelling. :-)

          -Mike


          Comment

          • Peter Julian

            #6
            Re: difference between a structure and a class


            "Shea Martin" <sam_squirrelnu tz@hotmail.com> wrote in message
            news:h1%ne.4139 4$Ph4.937036@ur sa-nb00s0.nbnet.nb .ca...[color=blue]
            > I have been programming in C++ for over 4 years. I *think* I knew that
            > a struct could have a constructor but I decided to dig into it a little
            > more today, and found that there is very little difference between a
            > struct and a class in C++. Both have inheritance, access modifiers,
            > etc. The only diff I see is the default access level is public vs.
            > private.
            >[/color]

            A struct and a class is exactly the same thing (called an abstract type) so
            long as we are confining the definition to the C++ language. The only
            difference is the default access given to their members, ctors, dtor and
            member functions.

            Your statement about constructors is misleading, a class or struct *must*
            have a constructor and a destructor, there is no choice offered here. The
            question is: will the compiler generate defaults for you or did you choose
            to provide the d~tor and ctors yourself? If you supply a single constructor
            with arguement(s), for example, the compiler ceases to supply any default,
            non-copy constructor. (see code below)

            The compiler provides a default shallow copy-ctor and an assignment operator
            as well. The compiler will always supply these until you overide the
            compiler's responsability to do so. Again: same for struct and class.
            [color=blue]
            >
            > I have always just used structs as a minimal class, as that is the way
            > they were used in textbooks.
            >
            >
            >
            > Do structs have all these features in pure C? If not then why did c++
            > change them, when they were adding the Class type? Are there more
            > differences than those I have listed? Performance differences? I have
            > always just used structs as a minimal class, as that is the way they
            > were used in textbooks.[/color]

            No difference at all between the two. One uses the keyword struct and the
            other class. Even inheritence applies to both with respect for their access
            defaults.

            Note how the following Abstract class is not sufficient to create an array
            of Abstract objects. An array requires a default constructor (thats the
            law). The default constructor can't be supplied here because the compiler
            acknowledges that the creator of the class has taken-over the responsability
            to supply all non-copy constructors.

            The following won't compile unless you uncomment the default ctor (or
            comment-out all ctors for the compiler to generate them for you).

            #include <iostream>

            class Abstract
            {
            int a; // private
            public:
            // ctors
            // Abstract() { std::cout << "default ctor invoked\n"; } // uncomment
            this ctor
            Abstract(int n) : a(n) { std::cout << "ctor invoked\n"; }
            // d~tor
            ~Abstract() { std::cout << "\nd~tor invoked"; }
            // copy-ctor
            Abstract(const Abstract& r_copy)
            {
            std::cout << "copy-ctor invoked\n";
            a = r_copy.getA();
            }
            // member function (accessor)
            int getA() const { return a; }
            };


            int main()
            {
            Abstract array[5]; // an array of 5 default Abstract objects
            Abstract abstract(99); // a single Abstract object with member a = 99
            Abstract copy_of_abstrac t(abstract); // copy
            array[0] = copy_of_abstrac t; // assignment

            std::cout << "array[0].getA() = " << array[0].getA();

            return 0;
            }

            Comment

            • EventHelix.com

              #7
              Re: difference between a structure and a class

              Classes and structures also differ in terms of the memory
              layout of member variables. In a simple structure,
              the memory layout is in the order of the fields. In a class
              the memory layout is undefined and compiler dependent.

              --
              EventStudio 2.5 - http://www.EventHelix.com/EventStudio
              Auto Layout and Generate Sequence Diagrams in PDF and MS Word

              Comment

              • John Carson

                #8
                Re: difference between a structure and a class

                "EventHelix.com " <eventhelix@gma il.com> wrote in message
                news:1117851521 .431542.122310@ g44g2000cwa.goo glegroups.com[color=blue]
                > Classes and structures also differ in terms of the memory
                > layout of member variables. In a simple structure,
                > the memory layout is in the order of the fields. In a class
                > the memory layout is undefined and compiler dependent.[/color]

                In circumstances in which a struct is guaranteed to have members laid out in
                the order of declaration, an otherwise identical class will likewise have
                members laid out in the order of declaration provided the class begins with
                the keyword public:, as in

                class X
                {
                public:
                // stuff
                };

                In other words, the only difference is one of default access specifiers.
                Adding public: changes the class's access and makes it equivalent to a
                struct for layout purposes. If a struct has virtual members etc., then its
                member layout is no longer guaranteed, just as with a class that has virtual
                members etc.

                --
                John Carson

                Comment

                • Alan Johnson

                  #9
                  Re: difference between a structure and a class

                  Shea Martin wrote:
                  [color=blue]
                  > Thoughts, Ideas?[/color]

                  Just a bit of trivia: A union can also have member functions (including
                  constructors and destructors) in C++. It cannot, however, participate in
                  inheritance or have virtual functions.

                  -Alan

                  Comment

                  • John Carson

                    #10
                    Re: difference between a structure and a class

                    "Alan Johnson" <alan@undisclos ed.com> wrote in message
                    news:d7s5r3$1uj $1@news.Stanfor d.EDU[color=blue]
                    > Shea Martin wrote:
                    >[color=green]
                    >> Thoughts, Ideas?[/color]
                    >
                    > Just a bit of trivia: A union can also have member functions
                    > (including constructors and destructors) in C++.[/color]


                    True. However:

                    Section 9.5/1
                    "An object of a class with a non-trivial constructor (12.1), a non-trivial
                    copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
                    copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
                    can an array of such objects."

                    --
                    John Carson

                    Comment

                    • Ron Natalie

                      #11
                      Re: difference between a structure and a class

                      EventHelix.com wrote:[color=blue]
                      > Classes and structures also differ in terms of the memory
                      > layout of member variables. In a simple structure,
                      > the memory layout is in the order of the fields. In a class
                      > the memory layout is undefined and compiler dependent.
                      >[/color]
                      Not true. The ONLY difference is the default access. The
                      memory layout is the same. Between access specifiers, they
                      are laid out in increasing memory address (but their may be
                      interspersed padding).

                      Comment

                      • DeltaOne

                        #12
                        Re: difference between a structure and a class

                        Hi ,
                        There is only the access qualifiers that differ in both. In C++ default
                        is private as Class is designed for data hiding and struct default is
                        public... Tehre is no difference. It may happen if u are constructing a
                        compiler for C++ that for both delarations u can replace with a single
                        name say CLST


                        like for
                        class example{
                        ....
                        }

                        struct exmple1{
                        .....
                        }

                        both wil be translated to

                        CLST example{
                        ....
                        }
                        and
                        CLST exmple1{
                        ....
                        }

                        but u need to take care for the default access qualifiers.. Thats all
                        struct was insrduced for the only fact that it was at that time people
                        or C programmers adore struct more than class.. Introduction of struct
                        has more to do with mentality of people rather than technical.

                        Bye

                        Comment

                        Working...