{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS31-6

# CS31-6 - CS 31 Introduction to Computer Science I Topic 6...

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

CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-1 CS 31 : Introduction to Computer Science I Topic 6 Structs and Classes

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

View Full Document
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-2 Structs Vectors: One Data Type A C++ vector is an aggregate data type of multiple data items of the same type Elements of a vector are accessed by using an index
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-3 Structs Different Data Types Some programs have multiple data items of different types which can be grouped together into a single data entity A vector cannot be used because all the elements of a vector must be the same type of data (e.g. integer)

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

View Full Document
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-4 Structs Structs: Multiple Data Types A C++ struct is an aggregate data type of multiple data items which can hold items of different types Elements of a struct are accessed by using a name
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-5 Structs Struct Definition The definition of a struct specifies the names and types of its member elements Each member element has a unique name and a type Member elements can be any type of C+ + data including simple variables, vectors, and even other structs

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

View Full Document
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-6 Structs Struct Definition Example struct TestData { int testNumber; float voltage; float current; float power; };
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-7 Structs Struct Definition Details The definition of a struct begins with the keyword struct followed by its name An open brace precedes the declarations of the member variables Each member variable is declared in order by type of data and member name

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

View Full Document
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-8 Structs Struct Definition Details The end of the member variable declarations is followed by a close brace The definition must end with a semicolon The definition is only a pattern No actual data is created by the definition Although technically allowed, variables are created in declarations separate from the definition of the struct
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A. Rohr All Rights Reserved JAR 6-9 Structs Struct Use Once defined, an instance of a struct is declared just like any other variable The name of the struct is used like a type Any number of instances of the struct may be declared in a program Each one must have a different name

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

View Full Document
CS 31: Introduction To CS I Topic 6: Structs and Classes 9/2009 John A.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}