Menu

What's the difference between class and struc

2004-05-04
2012-09-26
  • Nobody/Anonymous

    The tutorial I am reading suddenly introduces Struct without explaining it.  Prior to this I learned about classes and to be honest a struct looks to be just the same thing.

    In the example from the tutorial (see below) I can replace the word struct with the word class and the program still compiles and runs. 

    So what's the difference?

    Thanks In Advance
    dicky

    //---------------------------------------------------------------------------
    #include <iostream>
    #include <iomanip>
    using namespace std;
    struct TBrick
    {
    public:
        void Initializer(double l, double h, double w);
        double Volume() const;
        void Properties() const;
    private:
        double Length;
        double Height;
        double Width;
    };
    //---------------------------------------------------------------------------
    void TBrick::Initializer(double l, double h, double w)
    {
        Length = l;
        Height = h;
        Width = w;
    }
    //---------------------------------------------------------------------------
    double TBrick::Volume() const
    {
        return Length * Height * Width;
    }
    //---------------------------------------------------------------------------
    void  TBrick::Properties() const
    {
        cout << "Red Brick Properties";
        cout << setiosflags(ios::fixed) << setprecision(2);
        cout << "\nLength = " << Length;
        cout << "\nHeight = " << Height;
        cout << "\nThickness = " << Width;
        cout << "\nVolume = " << Volume() << "\n";
    }
    //---------------------------------------------------------------------------
    int main(int argc, char* argv[])
    {
        TBrick RedBrick;

        RedBrick.Initializer(124.45, 56.25, 32.55);
        RedBrick.Properties();
       
        return 0;

     
    • Nobody/Anonymous

      A class and a struct are almost exactly the same thing.

      Differences are:

      (1a) struct - all members are public by default
      (1b) class - all members are private by default

      (2a) class - has default constructors and destructors created for it
      (2b) struct - no default constructor destructor (in fact I believe that even if you wrote constructors/desctructors for it that they would not be invoked).

      There may be some others, but these are the one's I always keep in mind.

      Although you can put functions into a struct, I would recommend against it. In general, structs are less useful than classes and I would counsel using classes instead of structs (although we can argue about efficiency concerns).

      rr

       
    • Wayne Keen

      Wayne Keen - 2004-05-04

      From a very top level viewpoint, I view structures as a way that C (and other languages that have them) package data together.  Classes are an extension of that, a way of packaging data and methods together.  So you can see how C++ could simple do some extensions on the concept of a structure, and get a class/object.

      rr did a good job of giving you the real differences in case you found my babbling at too high a level (like what planet we are on)

      Wayne

       
    • Nobody/Anonymous

      Just did a quick test, took an app with a class, changed the word class to struct, and put messagebeeps in the constructor and destructor to indicate if they're called, the program ran fine and beeped both on construct and destruct, so they are invoked.
      I'd say unless you're just using it for data with no functions stick with using class.

      I actually have a book on C++ that uses structs instead of classes throughout it for some reason.. always thought that was weird..

      b_a

       
      • Nobody/Anonymous

        I'm really surprised that the constructors and destructors were invoked on a struct.

        I suppose I could have gotten up and fired up the compiler and tested it out myself ... but that would involve effort ... much better to sit around and engage in endless sophistry ...

        Thanks for doing the hard legwork b_a.

        rr

         
    • Nobody/Anonymous

      Wow - thanks guys for the swift and informative answers.

      dicky

       
    • Nobody/Anonymous

      Of course I should have qualified that it works with GCC 3.2, but could be implimentation specific.. if someone wants to do the real hard work they could dig through the standard and see what it says.
      I don't recall any of the books I've read saying anything one way or the other about constructors/destructors in structs, they just point out the public/private difference between classes and structs.

      black_adder

       
    • Nobody/Anonymous

        Classes support inheritance and other object-oriented properties.  Structs don`t.

                                         Alan

       
    • qWake

      qWake - 2004-05-04

      Hmmm, there is a good deal of incorrect/incomplete information in this thread...

      In C++, a struct IS a class.  There is nothing that can be done with a class that cannot also be done with a struct.  If you look at the C++ Standard, you will see plenty of discussions on classes where the sample code actually uses a struct (so that the "public:" tag is not needed) since it has the same effect.  The only difference is that members of a struct are public by default and members of a class are private by default.  This:

      class MyClass {

      ...is semantically equivalent to this:

      struct MyClass { private:

      Or if you want to see it the other way, this:

      struct MyStruct {

      ...is semantically equivalent to this:

      class MyStruct { public:

      The above is for C++.  As far as plain C is concerned then yes, there are much more serious differences: classes don't exist!  None of the OO concepts work, so in plain C a struct is just a structure without functions.  So no constructor, destructor and so on, no inheritance, no polymorphism...

      qWake

       
      • Wayne Keen

        Wayne Keen - 2004-05-05

        So I got it more or less right?

        Wayne

         
        • qWake

          qWake - 2004-05-05

          I'd say you did, yes.  Stroustrup implemented C++ classes by extending the existing C struct.  Why he found it necessary to introduce the new "class" keyword is unclear to me since it is in fact redundant in the presence of "struct".  Maybe his first idea was to have different capabilities for both, until he realized that there was no need to make the distinction...  Just speculating.

          qWake

           
          • qWake

            qWake - 2004-05-05

            I decided to quell my curiosity and find the answer to my own question, which I found here:  http://cpptips.hyperformix.com/cpptips/struct_vs_class

            The keyword 'class' was introduced to help people focus on the distinction between a structure in plain C and its more advanced capabilities in C++.  The author found that it would assist learning the new features and use them correctly.  That's pretty much all, it is otherwise a language redundancy.

            qWake

             
            • Wayne Keen

              Wayne Keen - 2004-05-05

              You are a gentleman and a scholar sir.

              Wayne

               
    • Nobody/Anonymous

      I believe my knowledge of class / struct matches qWake's.

      http://www.csci.csusb.edu/dick/c++std/cd2/class.html

      Check out point 4.

      JMan

       
    • Amanda Wee

      Amanda Wee - 2004-05-05

      "if someone wants to do the real hard work they could dig through the standard and see what it says."

      ISO/IEC 14882:2003(E) gives:
      9.4
      A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11).

       
    • aditsu

      aditsu - 2004-05-05

      qwake and Eugene are right
      the only differences between structs and classes are:

      1. in C++, "struct xyz{" is IDENTICAL to "class xyz{public:"
      so a struct has constructors, destructors, inheritance, even multiple and virtual inheritance; you can write template structs, etc. etc., everything you can do with classes you can do with structs

      2. in C there is no such thing as "class", and structs exist but can contain only data members (i.e. no functions - that also means no ctor/dtor), and there's no inheritance and no templates
      they are also called POD (plain old data) structs

      some programmers use the "struct" keyword for C-style (POD) structs in C++ and the "class" keyword for true classes, but for the compiler there's no difference (except the public access in structs)

      Adrian

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.