CS31-9 - CS 31: Introduction to Computer Science I Topic 9...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-1 CS 31 : Introduction to Computer Science I Topic 9 Structs and Classes
Background image of page 1

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

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

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-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
Background image of page 4
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-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, arrays, and even other structs
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-6 Structs Struct Definition Example struct TestData { int testNumber; float voltage; float current; float power; };
Background image of page 6
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-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
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-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
Background image of page 8
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr All Rights Reserved JAR 9-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
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 9: Structs and Classes 1/2010 John A. Rohr
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 65

CS31-9 - CS 31: Introduction to Computer Science I Topic 9...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online