locked
Difference b/w structure n class... RRS feed

  • Question

  • wats the main difference b/w structure n class...

    Wednesday, October 17, 2007 7:35 AM

All replies

  • Class has method but stucture doesn`t

    and class represents OOP`s paradigm....

    Wednesday, October 17, 2007 7:56 AM
  • Hey,

    The main difference will be...

    Class is a reference type and Structure is value type.

     

    Mayur Tendulkar

    Microsoft Student Partner Lead | Microsoft India Pvt. Ltd. | +91 9740477233

    Wednesday, October 17, 2007 9:39 AM
  • Structure:

    Initially (in C) a structure was used to bundle different type of data types together

    to perform a particular functionality. But C++ extended the structure to contain

    functions also. The major difference is that all declarations inside a structure are

    by default public.


    Class:

    Class is a successor of Structure. By default all the members inside the class are

    private.

     

    Wednesday, October 17, 2007 2:01 PM
  • All members of a class is private by default but in structure all members are public

     

    Manoj

     

    Wednesday, October 17, 2007 2:36 PM
  • 1) The class is always having private all members by default,while

         structure is always having public all members by default

    2) We can make a functin member of a class but structure can't do it.

     

    Wednesday, October 17, 2007 3:51 PM

  • now i m getting confusion....
    Thursday, October 18, 2007 10:28 AM
  • CONFUSION :

     

    1 Some ppl say Class can have member functions, but Structure cannot.

    2 Some say Structures can also have member functions.

     

    ANSWER :

     

    The (2) is correct. Structures can also have member functions.

    All the declarations in a Structure are (by default) public.

    Thursday, October 18, 2007 4:21 PM
  •  abdul_rahim_8bf8da wrote:

    wats the main difference b/w structure n class...



    Here is the answer:-

    The fundamental issue is that of maintaining backwards compatability with C. When Bjarne created C++ (under the old name C with Classes) he wanted to emulate Smalltalk's object model in C. One of the important features Bjarne was adding to C++ was the concept of accessor privelege (private, public and protected.) When those things are added to a language, it is generally done such that private is the default.

    In order for code from C to work with C++, the members of a structure would be expected to be public. However, Bjarne knew that default privacy was important. The solution chosen was to clone the "struct" keyword and make this one small change. In fact structs and classes are the same thing; you can inherit structs from classes and vice versa (though it's pretty stupid, it's legal.) The ONLY technical difference between structs and classes is whether members default to private or public accessorship. This can be derived from the language of section 3.1 of the C++ standard (ISO IEC 14882-1998.)

    Repeat with me: "The only difference in C++ between struct and class is that struct is default public, whereas class is default private."

    Value and Reference Typing

    "''structures are value typed where as classes are refernce typed''"
    '''Incorrect'''. This was the case in Microsoft Visual C++ 5 and 6. The problem is that MSVC5 and 6 are older than C++; they implemented speculative versions of the language before the standard came out. Both have quite a few errors which were good guesses but didn't pan out; this is one of them. An easier example is:

    for (int i=0; i<10; ++i) {} int i;

    The compiler will incorrectly flag that as a redeclaration and fail.

    Inheritance

    "''Class can be inherited But Structure can't be inherited''"
    '''False''', and easily tested.
    ��#include <iostream>

    ��struct base {

    ������public: virtual int foo() { return 5; }

    ��};

    ��struct child {

    ������public: int foo() { return 9; }

    ��};

    ��int main() { base* example = new child; std::cout << child.foo(); }

    Initialization

    "''In structures we cannot initilase the variable during the declaration while in classes we can.''"
    '''False'''. You may tuple initialize any structure or class which is pure POD, and you may not tuple initialize any structure or class which contains methods. However, you may initialize structures or classes with methods in their constructor instead.

    Anonymous Classes and Anonymous Structures

    "''Structure can be declared without a tag at the first time, but not in case of class.''"
    '''Wrong'''. The fault is in the example code. Old code:

    struct { int something; } sFoo;

    sFoo sfoo; // works fine



    class { int something; } cFoo;

    cFoo cfoo; // error: cannot instantiate

    The problem is not about classes and structures; it's about default publicity. Structures are default public and classes are default private. When declared in that fashion, since there is no explicit constructor, a constructor is generated by the compiler for each. The constructor for the structure is default public, so the structure constructs just fine. However, the constructor for the class is default private, meaning that at instantiation time, the programmer does not have the access privelege to construct it. The error that results in most compilers talks about being unable to instantiate the class, and that can easily be misunderstood as a code bug, when in fact it's a usage bug.

    Polymorphism

    "''Structure s does not support polymorphism while class does.''"
    Simply '''false''', and a common and often repeated misunderstanding. The test for inheritance above also happens to display polymorphism.
    Thursday, October 18, 2007 9:13 PM