CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2002
    Location
    india
    Posts
    15

    difference between a c++ structure and class

    what is the difference between a C++ structure and a class?

    What comes to my mind are
    1) structures in c++ doesnot provide datahiding where as a class provides datahiding

    2)classes support polymorphism, whereas structures donot

    Is there anything other than this?

  2. #2
    Join Date
    Aug 2000
    Posts
    18
    How about constructors, destructors, inheritance, and a "this" pointer inside methods.

  3. #3
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    By default everything in structure is public and everything in class is private, but you can change it.

    And one more thing, by default inheritance of structure is public and inheritance of class is private.

    Hope it helps.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: difference between a c++ structure and class

    Originally posted by rajalekshmy+r
    what is the difference between a C++ structure and a class?

    What comes to my mind are
    1) structures in c++ doesnot provide datahiding where as a class provides datahiding

    2)classes support polymorphism, whereas structures donot

    Is there anything other than this?
    No, you are wrong on both 1 and 2.

    There is no difference between a class and a struct except that by default, the members of a struct are public, and the members of a class are private. Also structs are by default inherited publicly and classes by default are inherited privately. Other than that, whatever you can do in a class, you can do in a struct.

    Hopefully you weren't asked this question on a job interview.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2002
    Location
    Atlanta,GA
    Posts
    262
    Originally posted by _illusioned
    How about constructors, destructors, inheritance, and a "this" pointer inside methods.
    Technically speaking though you can instantiate a structure with the new operator in the simulation of a constructor.
    Jared

  6. #6
    Join Date
    May 2002
    Location
    india
    Posts
    15
    1)can we provide datahiding with structures? if yes, how?
    2)can structures suport run time polymorphism? If yes, how?

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by rajalekshmy+r
    1)can we provide datahiding with structures? if yes, how?
    2)can structures suport run time polymorphism? If yes, how?
    By writing code? Again, whatever you can do in a class, you can do in a struct. Why not write code and see for yourself?
    Code:
    #include <iostream>
    using namespace std;
    
    struct Whatever
    {
          virtual void foo() { }
          virtual ~Whatever() { }
    };
    
    struct Derived : Whatever
    {
        void foo() { cout << "value=" << value; }
        Derived(int n) : Whatever(), value(n) { }
    
        private:
            int value;  // private data member
    };
    
    int main()
    {
        Whatever *pW = new Derived(10);
        pW->foo();  // virtual call
        delete pW;
        return 0;
    }
    Convinced now?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    May 2002
    Location
    india
    Posts
    15
    i am fully convinced of your explanation, but if a structure can do whatever a class do why should we have concept of class?

  9. #9
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    Because Bjarne Stroustrup want to make its memory structure compatable with C. So if you dont use virtual function and virtual inheritance then you C++ class and C structure follow the same memory model.

    For further informatoin take a look at

    Design and Evaluation of C++
    by
    Bjarne Stroustrup.

  10. #10
    Join Date
    May 2002
    Location
    india
    Posts
    15
    Do you mean C++ structure and C++ class donot follow the same memory model. (If they are following the same memory model the datastructure class would not have been introduced by stroustroup, that is what I am interpreting, is it right?)

  11. #11
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    Let me explain it in more detail. The C structure, C++ structure and C++ class shares the same memory model until you havent introduce virtual function or inherit virtual C++ class or structure.

    But if you introduce virtual function or virtual inheritance then it is no more compatable with C structure. But the C++ structure and C++ class remain same after introducing these.

    For more information take a look at

    Inside C++ Object Model
    by
    Stan B Lippman

    I have written one artical which discuss the memory model of C++ class and behaviour of virtual function, virtual pointer and virtual table in Visual C++ (the behaviour of virtual table is not standard, so different compiler may use different technique)

    http://www.codeguru.com/atl/ATL_UnderTheHood_1.html

    It might be helpful to you.

    I again suggest to read "Design and Evaluation of C++" by Bjarne Stroustrup to understand the reason of each functionality of C++.

    Hope it helps.

  12. #12
    Join Date
    May 2002
    Location
    india
    Posts
    15
    yes it is of real help,let me go throu stroustroup

  13. #13
    Join Date
    Feb 2001
    Posts
    2,455
    Bjarne Stroustrup :

    "By definition, a struct is a class in which members are by default public; that is,
    struct s{ ....
    is simply shorthand for
    class s{public......
    "


    This tells me that there is no difference between struct and class, except as mentioned previously, by default everything in a struct is public and everything in a class is by default private.

    Mike B

  14. #14
    Join Date
    Mar 2008
    Posts
    3

    Smile Re: difference between a c++ structure and class

    Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.

    I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the same

  15. #15
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: difference between a c++ structure and class

    structs by default inherit publicly and classes by default inherit privately.
    Code:
    struct MyStruct {};
    class MyClass {};
    struct MyDerivedStruct : MyClass //Public inheritance.
    {}
    class MyDerivedClass : MyStruct //Private inheritance
    {}
    Quote Originally Posted by ramacet
    Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.

    I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the same
    One word - legacy reasons. Initially C had structs which could hold only data members and did not have access specifiers and all that good stuff. Then, when C++ was being designed, the creators wanted much more from a simple struct. They couldn't very well just break existing code by changing structs both syntactically or semantically. Thus, the class was born. Later on, more power was given to structures while maintaining them almost compatible with legacy code.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured