answersLogoWhite

0

Computer Programming
C++ Programming
The Difference Between

What is the difference between structures and class?


Top Answer
User Avatar
Wiki User
Answered 2011-09-14 09:33:37
HedgingI 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. BackgroundThe 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 answerThe 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 informationSeveral 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

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. AnswerIn structure,the default access specifier is public,where in class the default access specifier is private.
234
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Loading...

Still have questions?

Related Questions

What is difference between structure and Class in CPP?

members defined in structures are public by default and in class its private.class can have member functions which operate on those data members but structures have only data members


What is the difference between a building and a structure?

All buildings are structures but not all structures are buildings


Difference between c plus plus class and c structures?

A C struct can contain only data; a C++ class can contain both data and functions.


What is the difference between Lewis dot structures and Lewis structures?

These are synonyms.


Difference between first class and first class ac in railways?

No much difference between first class and first class ac. Only AC is the difference. First class does not have AC.


What's the difference between the Class 365 and Class 465 466?

The difference between the Class 365 and Class 465, 466 is basically the cost.


Difference between exclusive or inclusive class intervals?

difference between inclusive and exclusive class interval


What is difference between first class and second class on train trips?

What is the difference between buying a 1st class and a 2nd class ticket on the train in England?


What is the difference between a class What is the difference between a class III and class IV hitch and?

class III rated for up to 5,000lbs, class IV is rated for up to 10,000lbs towing capacity


What is the difference between strings and string arrays?

What is your understanding of structures? Discuss how structures work in programming


What is the What is the difference between class and object?

An object is an instance of a class


What is the difference between abstract class and final class?

20


What is the difference between class c and d felonies?

i need to know the difference between class c and d felonies please


Difference between Mercedes e class elegance and classic?

What is the difference between Mercedes Benz E class 'Elegance'and classic


What is the difference between man made and natural structures?

man-made structures were made by humans, but natural structures made itself.


What is the difference between classes of IP addresses?

difference between ip address and class


What is difference between A-class and B-class pipe?

Pipe ERW black pipe class -B And Class-C difference.


What is the difference between class intervals and class width in statistics?

NONE


What is the difference between a first class and a second class lever?

thanks


Difference between class 1 and class 2 preservatives?

znd


What is the Difference between b and c class MS pipe?

The main difference is the thickness of the pipe Compare to A Class pipe B Class pipe thickness is more.Like that Compare to B class pipe C Class pipe thickness is more.


What is the difference between socioeconomic status and social class?

i think there is no difference between two concepts.


What is the difference between natural and built structures?

the difference is that natural forces are forces that are created by the enviorment and built structures are not they are built by people #if you bleave in god he will tell you


What is the difference between homologous analogous and vestigial structures?

pickles :3


Difference between structures and class?

Structure members are public by default while class members are private by default. Classes encapsulate the data and the methods that operate upon that data into a discrete package (an object), exposing only as much or as little interface as is required by the class itself, to ensure the data remains in a valid state at all times. Structures have no such protection.