• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why do we need Structures in C++ when there is Class ?

 
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Structures and Unions are for grouping data members to create a new data type. Union uses a shared memory, unlike Structure.
Could anyone explain me the need to use Structures when a class is present  Thanks in advance
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want a union? I suspect it was only introduced into C in 1972 because memory was very expensive, but I am by no means certain.
Agree: if C++ has classes, who needs structs (not structures) any more? C++ still has structs because it is a strict superset of C and maintains all the features of C.
I hardly know any C++ but I can see no point in using either feature. Just because you have a feature, that doesn't mean you have to use it.
 
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why would you want a union? I suspect it was only introduced into C in 1972 because memory was very expensive


Memory might not be expensive now, but there are still plenty of programmable devices that don't have much of it. And a lot more that don't have as much as you need

And why waste memory when logically you don't need  to? If you have a structure that can only hold one object of different types at any one time, a union is the logical choice - why use something logically different that can hold lots of those different type objects?

There are also probably scenarios where a union works better with some hardware you are attempting to access.

C++ still has structs because it is a strict superset of C and maintains all the features of C.


Not true - quick google: https://www.quora.com/Can-we-say-C++-is-super-set-of-C

Personally probably the most annoying thing I've found when converting C to C++ is the latter's lack of designated initialisers, which means C code:
has to be re-written in C++ as:
In the latter, the values in the {} must match the order of the fields in the struct. Hence the C version is safer in that it remains correct if the order of the the fields in point is reversed. If feels like a step backwards; I think (should google it) it was removed to encourage people to use constructors to do initialisation, which I understand, but it's still a PITA
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Structs are also still present in modern languages like C#. The reason is invariably performance.

I've heard that in applications that do intensive graphics processing, dereferencing pointers all the time would become very expensive. Since structs aren't reference types, you manipulate the "object" directly.

I still avoid them most of the time though.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unions aren't just for saving memory. They also allow polymorphism in overlay form. For example, a network packet laid out in RAM might use different parts of the union depending on the packet type.

As for C++ and structs, you had to be there.

When Bjarne Stroustrup invented "C with classes" at AT&T Bell Labs, he wasn't inventing a language from scratch. Instead, he did like more than one earlier language designer did and implemented it as a pre-processor to convert his enhanced C into traditional C so that he could use the Unix C compiler to actually do the heavy lifting (just as early Unix C compilers translated C to assembly to let the assembler do the grunt work).

So to build the concepts of C++ classes, he started out by enhancing structs. In fact, a struct and a class have the same semantics, except that a struct has an implicit default member visibility of public and a class has an implicit default member visibility of private. If my rotting memory fails me not, you can even "subclass" structs - or at least could on the original language implements.

One of the key differences between C/C++ and Java is that a Java class is a data structure, but a C struct/class is a storage structure. Both are ordered collections of heterogeneous data, but a storage structure has a direct calculable mapping to specific areas of memory (relative to the object base), but a data structure is only a logical construct, not a physical one and the locations of its elements in memory are not meant to be predictable.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Structures are the basis on which classes were built. Classes allowed the members to be private and to place functions inside the classes. Classes are advancement of structures. Structure still exist in C++ because C++ is a superset of C language. When you write program in any C++ softwares. They allow you to write C programs as well so structures exist that in case you are working in C language and not using any C++ feature.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NMM, welcome to the Ranch
 
NaveenMishra Mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks....
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hello, Main Different Between structure and a class is that structure members have public access by
default and class members have private access by default,
Structure :- In structure have a by default public.In class have a by default private.
Structure cannot be inherited. But class can be inherit.
There is no data hiding features comes with structures. Classes do, private, protected and public.
A structure can't be abstract, a class can.
A structure is a value type, while a class is a reference type.

 
reply
    Bookmark Topic Watch Topic
  • New Topic