Composite type
Encyclopedia
In computer science
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

A struct is C
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....

'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 class
Class (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 structs.

Note that while classes and the class keyword
Keyword (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 structs. For all intents and purposes, C++ structs form a superset
SuperSet
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 structs: virtually all valid C structs are valid C++ structs with the same semantics.

Declaration

A struct 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:

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 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:

typedef struct Account_ {
int account_number;
char *first_name;
char *last_name;
float balance;
} Account;


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:

struct Vector {
float x;
float y;
float z;
};


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:

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 Vertex composite type, using the previously created Vector and Color composite types:

struct Vertex {
Vector position;
Color color;
};

Instantiation

Create a variable of type Vertex using the same format as before: Vertex v;

Member access

Assign values to the components of v like 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 struct is for the construction of complex datatypes, but sometimes it is used to create primitive structural
Structural 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


struct 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 type
Function 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:


typedef struct {
int x;
int y;
} Point;

typedef double (*Metric) (Point p1, Point p2);

typedef struct {
Point centre;
double radius;
Metric metric;
} Circle;
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK