|
In computer science, composite types are datatypes which can be constructed in a programming language out of that language's primitive types and other composite types. The act of constructing a composite type is known as composition.
C/C++ structures and classes
A In C++, the only difference between a Note that while classes and the DeclarationA For example: struct Account { int account_number; char *first_name; char *last_name; float balance; }; defines a type, referred to as Since writing For example: typedef struct Account_ { int account_number; char *first_name; char *last_name; float balance; } Account; In C++ code, the 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 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 struct Vertex { Vector position; Color color; }; InstantiationCreate a variable of type Member accessAssign values to the components of 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 SubtypingThe primary use of 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 typesFunction 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 |
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net