CPS120: Introduction to Computer Science - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CPS120: Introduction to Computer Science

Description:

The dot operator simply us to reference individual members of a structure Composite Data Types Nested Structures You ... Introduction to Computer Science Using ... – PowerPoint PPT presentation

Number of Views:169
Avg rating:3.0/5.0
Slides: 13
Provided by: wcc79
Learn more at: http://space.wccnet.edu
Category:

less

Transcript and Presenter's Notes

Title: CPS120: Introduction to Computer Science


1
CPS120 Introduction to Computer Science
  • Structures

2
Using enum
  • enum allows you to create your own simple data
    types for special purposes
  • Create a type
  • Give it a name
  • Specify values that are acceptable
  • enum sizes small, medium, large, jumbo
  • The compiler assigns an integer to each enum item

3
Composite Data Types
  • Records
  • A record is a named heterogeneous collection of
    items in which individual items are accessed by
    name
  • The elements in the collection can be of various
    types

4
Structures
  • Structures group variables together in order to
    make one's programming task more efficient.
  • Any combination of variables can be combined into
    one structure.
  • This is a useful and efficient way to store data.
  • struct Student
  • string socSecNum
  • string lastName
  • string firstName
  • int pointsEarned
  • double gpa

5
Using Structures
  • Each of the different variables are called
    members of the structure
  • Structures allow us to keep related data
    referring to individual members together
  • Strings, integer, and floating-point variables
    may be grouped together into one structure.
  • In effect, we have created our own customized
    data type.
  • The semicolon after the closing curly brace is
    required

6
Composite Data Types
7
Using the new data structure
  • The structure definition should be placed above
    the main function of a program but below the
    compiler directives
  • Declare an actual variable of this
    programmer-created data type within a function
    (such as main) in order to make use of this
    structureDone with a declaration statement like
  • Student freshmen
  • This reates a variable called freshmen of the
    data type Student

8
Assigning values to the structure
  • To assign a grade point average (GPA) of 3.4 to
    the gpa member of the variable freshmen, use the
    statement
  • freshmen.gpa 3.4
  • The period (.) that is used between the variable
    name freshmen and the member gpa is called the
    dot operator.
  • The dot operator simply us to reference
    individual members of a structure

9
Composite Data Types
Page 259
10
Nested Structures
  • You can use a previously defined structure as a
    member of another structure
  • Address is nested inside of the Customer
    structure.
  • Since Address is used within Customer, the
    structure definition for Address must be placed
    above Customer in order to avoid compile errors
  • struct Address string street string
    city string state int zip struct
    Customer string name string phone
    Address homeAddress Address businessAddress

11
Pointer Use in C.
  • A pointer is a variable or constant that holds a
    memory address
  • a) Hexadecimal numbers are used for representing
    memory locations

216793 216794 216801 iptr 216801 3 i 216802
12
Classes
  • The definition of an object is know as a class
  • It is similar to using basic data structures in
    C
  • When you declare an object, you are said to have
    instantiated it (given it instances)
  • Objects are members of a class
  • Paul Millis, George Bush and George Washington
    being members of the human being class
  • The design of a class is as important as its
    implementation
Write a Comment
User Comments (0)
About PowerShow.com