Composite type
Encyclopedia
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
.
'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
In C++, the only difference between a
is the default access level, which is private for classes and public for
Note that while classes and the
were completely new in C++, the C programming language
already had a crude type of
of C
For example:
defines a type, referred to as
which has an integer component, accessed by
Since writing
For example:
In C++ code, the
is not needed because types defined using
As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:
A variable named
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
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.
s (or type signature
s) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types:
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...
, a composite data type is any data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
which can be constructed in a program using its programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
's primitive data types and other composite types. The act of constructing a composite type is known as composition
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...
.
C/C++ structures and classes
Astruct
is CC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
's and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
'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
struct
keyword used in declaring them, which is short for structure or, more precisely, user-defined data structure.In C++, the only difference between a
struct
and a classClass (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
is the default access level, which is private for classes and public for
struct
s.Note that while classes and the
class
keywordKeyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....
were completely new in C++, the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
already had a crude type of
struct
s. For all intents and purposes, C++ struct
s form a supersetSuperSet
SuperSet Software was a group founded by friends and former Eyring Research Institute co-workers Drew Major, Dale Neibaur, Kyle Powell and later joined by Mark Hurst...
of C
struct
s: virtually all valid C struct
s are valid C++ struct
s with the same semantics.Declaration
Astruct
declaration consists of a list of fields, each of which can have any type. The total storage required for a struct
object is the sum of the storage requirements of all the fields, plus any internal padding.For example:
defines a type, referred to as
struct Account
. To create a new variable of this type, we can write struct Account myAccount;
which has an integer component, accessed by
myAccount.account_number
, and a floating-point component, accessed by myAccount.balance
, as well as the first_name
and last_name
components. The structure myAccount
contains all four values, and all four fields may be changed independently.Since writing
struct Account
repeatedly in code becomes cumbersome, it is not unusual to see a typedef
statement in C code to provide a more convenient synonym for the struct
.For example:
In C++ code, the
typedef
Typedef
typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.Under C convention , types...
is not needed because types defined using
struct
are already part of the regular namespace, so the type can be referred to as either struct Account
or simply Account
.As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:
A variable named
velocity
with a Vector
composite type would be declared as Vector velocity;
Members of the velocity
would be accessed using a dot notation. For example, velocity.x = 5;
would set the x
component of velocity
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
Vertex
composite type, using the previously created Vector
and Color
composite types:Instantiation
Create a variable of typeVertex
using the same format as before: Vertex v;
Member access
Assign values to the components ofv
like so:Primitive subtype
The primary use ofstruct
is for the construction of complex datatypes, but sometimes it is used to create primitive structuralStructural type system
A structural type system is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not by other characteristics such as its name or place of declaration. Structural systems are used to determine if types are equivalent and whether a...
subtyping
Subtype
In programming language theory, subtyping or subtype polymorphism is a form of type polymorphism in which a subtype is a datatype that is related to another datatype by some notion of substitutability, meaning that program constructs, typically subroutines or functions, written to operate on...
. 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.
Function types
Function typeFunction type
In computer science, a function type is the type of a variable or parameter to which a function has or can be assigned or the result type of a higher-order function returning a function....
s (or type signature
Type signature
In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes at least the function name and the number of its arguments...
s) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types: