7_ complex data types

# 7_ complex data types - Structures y A structure is a...

This preview shows pages 1–8. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Structures y A structure is a collection of one or more variables grouped together under a single name he data elements in a structure are known as its ember y The data elements in a structure are known as its member variables or simply members , which can be of different types y Structures help to organize complicated data y Allow a group of related variables to be treated as a unit instead of separate entities ructures act like any basic data type y Structures act like any basic data type y May be copied and assigned to y May be passed to and returned by functions 1 Definitions y In C++, a structure is defined using the keyword struct , followed by the structure tag , a list of member variables (with pes and identifiers) enclosed within a pair of braces } nd types and identifiers) enclosed within a pair of braces { } , and a semicolon ; Syntax struct struct_tag { type_1 member_var_1; type_2 member_var_2; … y The structure tag serves as the identifier of the newly defined ructure type }; structure type 2 Definitions y A member variable and an ordinary variable can have the same identifier without conflict hey can be distinguished by ntext y They can be distinguished by context y Similarly, the same member identifier may occur in different structures y Examples: double radius; struct Circle { Member and ordinary variables can have the same identifiers double radius; int x, y; }; struct Point { t ; Member variables of the same type can be grouped into a single list separated by commas 3 int x; int y; }; The same member identifiers may occur in different structures Declarations y Once a structure type is defined, its structure tag can be used to declare variables of this structure type y xamples: Syntax struct_tag variable_name_1, variable_name_2, …; Examples: Point pt1, pt2; Circle c1, c2, c3; 4 Declarations y The declaration may also take place in the structure definition by putting the variable names before the semicolon ; after the ght brace right brace } Syntax struct struct_tag { pe 1 member var 1; type_1 member_var_1; type_2 member_var_2; … } variable_name_1, variable_name_2, …; y Examples: struct Point { int x; 5 int y; } pt1, pt2; Declarations y If the declaration only occurs once in the structure definition, the structure tag may be omitted in the definition Syntax struct { type_1 member_var_1; type_2 member_var_2; y Examples: … } variable_name_1, variable_name_2, …; struct { int x; int y; 6 } pt1, pt2; Initializations y A structure variable can be initialized in its declaration by using an equal sign followed by list of values given in the rder at corresponds to the order of the y A list of values given in the order that corresponds to the order of the member variables in the structure definition; or y Another structure variable of the same type ; or f t i th t t t t f th y A function that returns a structure of the same type y Examples: Point offset(Point x, Point y); // a function that returns a Member variables: // structure of type Point...
View Full Document

{[ snackBarMessage ]}

### Page1 / 31

7_ complex data types - Structures y A structure is a...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online