CS31-6

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

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 6-1 CS 31 : Introduction to Computer Science I Topic 6 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 9/2009 John A. Rohr All Rights 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
Background image of page 2
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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)
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 9/2009 John A. Rohr All Rights 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
Background image of page 4
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
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 9/2009 John A. Rohr All Rights JAR 6-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 9/2009 John A. Rohr All Rights 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
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 9/2009 John A. Rohr All Rights 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
Background image of page 8
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights 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
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 9/2009 John A. Rohr All Rights JAR 6-10 Structs Struct Member Access Member variables of a struct are accessed by the instance name and the name of the member A period is used to separate the two Each separate instance is a different set of data
Background image of page 10
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 65

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

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

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