- Composite data type
-
In computer science, a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types. The act of constructing a composite type is known as composition.
Contents
C/C++ structures and classes
For more details on this topic, see C++ classes.A
structis 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 thestructkeyword used in declaring them, which is short for structure or, more precisely, user-defined data structure.[citation needed]In C++, the only difference between a
structand a class is the default access level, which is private for classes and public forstructs.Note that while classes and the
classkeyword were completely new in C++, the C programming language already had a crude type ofstructs. For all intents and purposes, C++structs form a superset of Cstructs: virtually all valid Cstructs are valid C++structs with the same semantics.Declaration
A
structdeclaration consists of a list of fields, each of which can have any type. The total storage required for astructobject is the sum of the storage requirements of all the fields, plus any internal padding.For example:
struct Account { int account_number; char *first_name; char *last_name; float balance; };
defines a type, referred to as
struct Account. To create a new variable of this type, we can writestruct Account myAccount;which has an integer component, accessed bymyAccount.account_number, and a floating-point component, accessed bymyAccount.balance, as well as thefirst_nameandlast_namecomponents. The structuremyAccountcontains all four values, and all four fields may be changed independently.Since writing
struct Accountrepeatedly in code becomes cumbersome, it is not unusual to see atypedefstatement in C code to provide a more convenient synonym for thestruct.For example:
typedef struct Account_ { int account_number; char *first_name; char *last_name; float balance; } Account;
In C++ code, the
typedefis not needed because types defined usingstructare already part of the regular namespace, so the type can be referred to as eitherstruct Accountor simplyAccount.As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:
struct Vector { float x; float y; float z; };
A variable named
velocitywith aVectorcomposite type would be declared asVector velocity;Members of thevelocitywould be accessed using a dot notation. For example,velocity.x = 5;would set thexcomponent ofvelocityequal to 5.Likewise, a color structure could be created using:
struct Color { unsigned int red; unsigned int green; unsigned int blue; };
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
Vertexcomposite type, using the previously createdVectorandColorcomposite types:struct Vertex { Vector position; Color color; };
Instantiation
Create a variable of type
Vertexusing the same format as before:Vertex v;Member access
Assign values to the components of
vlike so:v.position.x = 0.0; v.position.y = 1.5; v.position.z = 0.0; v.color.red = 128; v.color.green = 0; v.color.blue = 255;
Primitive subtype
The primary use of
structis 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 codestruct ifoo_old_stub { long x, y; }; struct ifoo_version_42 { long x, y, z; char *name; long a, b, c; }; void operate_on_ifoo(struct ifoo_old_stub *); struct ifoo_version_42 s; . . . operate_on_ifoo(&s);
will work correctly.
Function types
Function types (or type signatures) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types:
typedef struct { int x; int y; } Point; typedef double (*Metric) (Point p1, Point p2); typedef struct { Point centre; double radius; Metric metric; } Circle;
See also
- Object composition
- struct (C programming language)
Data types Uninterpreted Numeric - Integer
- Fixed-point
- Floating-point
- Rational
- Complex
- Bignum
- Interval
- Decimal
Text Pointer Composite Other - Boolean
- Bottom type
- Collection
- Enumerated type
- Exception
- Function type
- Opaque data type
- Recursive data type
- Semaphore
- Stream
- Top type
- Type class
- Unit type
- Void
Related topics - Abstract data type
- Data structure
- Interface
- Kind
- Primitive data type
- Subtyping
- Template
- Type constructor
- Parametric polymorphism
Categories:- Data types
- Composite data types
- Type theory
- Articles with example C++ code
Wikimedia Foundation. 2010.