CPS120: Introduction to Computer Science - PowerPoint PPT Presentation

About This Presentation
Title:

CPS120: Introduction to Computer Science

Description:

CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings – PowerPoint PPT presentation

Number of Views:178
Avg rating:3.0/5.0
Slides: 36
Provided by: spaceWccn3
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
  • Lecture 16
  • Data Structures, OOP Advanced Strings

2
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
3
Using Pointers
  • Pointers must be initialized
  • e.g. iptr I
  • This reads iptr is assigned the address of i

4
Intializing Pointers
  • Declare pointers before use, as with other
    variables.
  • Each variable being declared as a pointer must be
    preceded by an asterisk ().
  • Initialize pointers before use to a 0, NULL or an
    address to prevent unexpected results

5
Pointer Operators
  • is the address operator which returns the
    address of its operand
  • is the indirection operator or dereferencing
    operator and returns the value to which the
    operand (pointer) points.
  • sizeof - used to determine the size of an array
    during program compiliation

6
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

7
typedef
  • typedef gives a new name to an existing data type
  • typedef float real
  • Confusing to the reader, should be used sparingly

8
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

9
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

10
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

11
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

12
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

13
CPS120 Introduction to Computer Science
  • Lecture 16B
  • Object-Oriented Concepts

14
The Procedural Paradigm
  • The functions and algorithms are the focus, with
    data viewed as something for the functions to
    manipulate

15
Object-Oriented Paradigm
  • Data should be placed inside the objects and that
    these objects should communicate with each other
    in the form of messages

16
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

17
Including Classes in C
  • For classes, the include directive uses
    different punctuation for header (.h) files
  • Quotation marks are used when the header file is
    a source file in the same location as the program
    source code
  • Angle brackets are used when the header file is
    one of the compiler's pre-compiled library
    functions

18
Designing a Class
  • Think in an object-oriented way
  • E.g. an answering machine encapsulates the
    functions of an answering machine with the data
    (messages).
  • The buttons are the equivalent of sending
    messages to the machine

19
Defining a Class
  • Functions and variables that are prototyped and
    declared in a class definition are called members

20
Public vs Private
  • Private Cannot be accessed outside the object
  • Public Can have access to all the variables and
    functions
  • public
  • // constructors
  • circle() // default
    constructor
  • circle(const circle ) // copy constructor
  • // member functions
  • void SetRadius(float)
  • double Area()
  • private
  • // data
  • float radius

21
Constructors
  • Allow all data encapsulated within an object to
    be initialized to preset values so that errors
    can be avoided

22
Member Functions
  • Provide a way for a programmer to pass data to
    and get data from an object
  • Implemented like a normal C function
  • Except -- The class name and the scope-resolution
    operator ( ) precede the function name
  • circle circle() // default constructor

23
OOP Advantages Reusability
  • Reusability is a major benefit of object-oriented
    programming

24
OOP Advantages Containment
  • Containment is a term used to describe an object
    that contains one or more objects as members
  • (Passes the 'has-a' rule)

25
OOP Advantages Inheritance
  • Inheritance is the term used to describe an
    object that inherits properties from another
    object
  • (Passes the 'is-a' rule)
  • The class from which an object inherits
    properties is called a parent class or base class
  • The class that inherits the properties is called
    a child class or derived class

26
CPS120 Introduction to Computer Science
  • Lecture 16C
  • Advanced Strings

27
Library functions manipulate strings
  • length function
  • stringLength userString.length( )
  • To compare two strings
  • if (string1 string2)
  • Test two strings alphabetically
  • if (string1 lt string2)
  • cout ltlt "string1 comes before string2
  • alphabetically "

28
Library functions manipulate strings
  • To compare two strings
  • if (string1 string2)

29
Library functions manipulate strings
  • Test two strings alphabetically
  • if (string1 lt string2)
  • cout ltlt "string1 comes before string2
  • alphabetically "

30
Concatenation
  • To combine two strings into one larger string
  • name "John"lastName "Doe"cout ltlt name
    lastName
  • The character array that is going to receive the
    additional characters must have enough memory
    allocated to hold the additional characters
  • The STRCAT function will continue to add
    characters even if it reaches the end of the
    destination character array
  • The result is that other memory is overwritten
  • with the remaining string data.

31
A String Class
  • A string class can make C strings much easier
    to use
  • a) A string class can provide bounds checking to
    avoid string errors
  • b) A string class can also allow the use of
    standard C operators when working with strings
  • i) , , and

32
Converting Strings
  • There are C library functions that allow you to
    convert numbers stored in strings to integer and
    floating point values
  • atoi-- numbers stored as characters can be
    converted for calculations
  • atol -- converts a string to a long integer
  • atof -- convers a string to a floating point
    number

33
Using A String Class
  • Required header file
  • include "oostring.h"

34
Instantiating a String Object
  • // Instantiate empty string object
  • oostring MyString1
  • // Initialize while instantiating
  • oostring MyString2("Hello, World!")

35
Assigning Values to Strings
  • MyString1 MyString2
  • MyString1 "string variable"
  • MyString1 'A'
Write a Comment
User Comments (0)
About PowerShow.com