Computer Programming
C++ Programming
The Difference Between

What is the difference between structures and class?

Answer

User Avatar
Wiki User
09/14/2011

Hedging

I have removed all but one answer while writing this. Please check my references to the c++ standard before removing this as vandalism; nearly every info bit on this page was incorrect or misunderstood to begin with.

Background

The question here regards specifically c++. This is not a cross-language question; neither struct nor class means the same thing from language to language, and c++ is the only language which has both in a way such that they are related (as well as its offshoots, such as concurrent c++.) This question is a common misunderstanding, though, for novice c++ programmers, and is frequently given in c++ faqs. Outside of c++, the answer is "those terms could mean anything."

Primary 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."

Extra information

Several misunderstandings were in place here. I will explain them one by one, so that their removal is not protested.

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.

POD versus UDT

"''A structure is an entity that is defined in terms of other structures or primitive datatypes. As a result, a structure is normally considered to be a complex-, or compound datatype. With structures the default access type is public. Classes are similar to structures in that they are made up of other datatypes, but they differ in one significant respect - classes contain information on the program functions (formally termed "methods") that will be used to manipulate the data that the class can store. The default access type for classes is private.''"'''This is a more complex issue'''. Whereas this is generally the case in code, that's by convention; the language makes no such requirement. In fact the author of this comment even tips his hat to that structures may have methods later, but the phrasing here suggests that this is part of c++, whereas in fact it is not. More clearly put, there is the concept of a "POD type." POD, or Plain Old Data, is any type which consists solely of inert storage - pointers, integers, arrays, that sort of thing. The alternative is called a UDT, or user-defined type; whereas most POD is user defined and whereas most POD is typal, what a user-defined type is meant to mean is a type which has user-defined behavior, ie something other than what the hardware does. Good examples are arithmetic number classes (a class which stores a numerator and a denominator, instead of the real value, so that there is no loss of precision) and many instances of the C++ string class, which frequently implement such things as shared buffers and lazy copies. The fundamental issue here is that most C++ programmers use the struct keyword to make POD and the class keyword to make everything else, and the behavior is so common that it's important to know it; however it's also important to realize that that's an idiom, not a language requirement, because sometimes POD should have methods like copy constructors, such as when copying the data requires things that aren't usually handled, like copying a word at a time over a limited-range bus.

Answer

In structure,the default access specifier is public,where in class the default access specifier is private.