Composite data type

In computer science, a composite data type or compound data type is any data type which can be constructed in a program using the programming language's primitive data types and other composite types. It is sometimes called a structure or aggregate type, although the latter term may also refer to arrays, lists, etc. The act of constructing a composite type is known as composition. Composite data types are often contrasted with scalar variables.

C/C++ structures and classes
A  is C's and C++'s notion of a composite type, a datatype that composes a fixed set of labeled fields or members. It is so called because of the  keyword used in declaring them, which is short for structure or, more precisely, user-defined data structure.

In C++, the only difference between a  and a class is the default access level, which is private for classes and public for  s.

Note that while classes and the  keyword were completely new in C++, the C programming language already had a raw type of  s. For all intents and purposes, C++  s form a superset of C  s: virtually all valid C  s are valid C++  s with the same semantics.

Declaration
A  declaration consists of a list of fields, each of which can have any type. The total storage required for a  object is the sum of the storage requirements of all the fields, plus any internal padding.

For example:

defines a type, referred to as. To create a new variable of this type, we can write which has an integer component, accessed by, and a floating-point component, accessed by  , as well as the   and   components. The structure  contains all four values, and all four fields may be changed independently.

Since writing  repeatedly in code becomes cumbersome, it is not unusual to see a   statement in C code to provide a more convenient synonym for the. However, some programming style guides advise against this, claiming that it can obfuscate the type.

For example:

In C++ code, the  is not needed because types defined using   are already part of the regular namespace, so the type can be referred to as either   or simply.

As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:

A variable named  with a   composite type would be declared as   Members of the   would be accessed using a dot notation. For example,  would set the   component of   equal to 5.

Likewise, a color structure could be created using:

In 3D graphics, you usually must keep track of both the position and color of each vertex. One way to do this would be to create a  composite type, using the previously created   and   composite types:



Instantiation
Create a variable of type  using the same format as before:

Member access
Assign values to the components of  like so:

Primitive subtype
The primary use of  is for the construction of complex datatypes, but sometimes it is used to create primitive structural subtyping. For example, since Standard C requires that if two structs have the same initial fields, those fields will be represented in the same way, the code

will work correctly.

Type signature
Type signatures (or function types) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types: