Arrays, Pointers and Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays, Pointers and Structures

Description:

street addresses in a telephone book point to where some lives in the community. a forwarding address points to an address of where some one can be reached ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 10
Provided by: lockh8
Category:

less

Transcript and Presenter's Notes

Title: Arrays, Pointers and Structures


1
Arrays, Pointers and Structures
  • CS-240
  • Dick Steflik

2
Pointers
  • allow us to access something indirectly
  • index of a book points to some topic in the body
    of a book
  • street addresses in a telephone book point to
    where some lives in the community
  • a forwarding address points to an address of
    where some one can be reached (pointer to a
    pointer)

3
Arrays and Structures
  • Arrays - ordered collections of objects, all of
    the same type
  • int abc4
  • Structure - a collection of objects of dissimilar
    types
  • struct MyType int abc double def

4
First Class Objects
  • First Class Object
  • can be manipulated in all of the usual ways
  • are usually safer than Second Class Objects
  • preserve value semantics
  • obj2 obj1 (assignment)
  • objtype a objtype b(a) (copy constructor)
  • a b (overloaded assignment operators)
  • usually implemented as a class
  • can have bounds checking

5
Second Class Objects
  • Primitive types (arrays and structs)
  • dont preserve value semantics
  • are not as safe as First Class Objects
  • no bounds checking on array indicies

6
Standard template Library
  • a standard set of templates and class templates
    that provide a set of First Class types
  • was an extra in older compilers
  • built into current generation of compilers

7
vector
  • vector is a template class that provides a First
    Class version of the array primitive
  • vectorltintgt a(3)
  • allocates a vector containing 3 integers
  • vector methods
  • size() - return of elements in the vector
  • resize(n) - change the size of the vector to n
    elements

8
include ltstdlib.hgt include ltiostream.hgt include
ltvector.hgt using namespace std int main()
const int DIFFERENT_NUMBERS 100 int
totalNumbers cout ltlt How many numbers?
cin gtgt totalNumbers vectorltintgt
numbers(DIFFERENT_NUMBERS 1) for (int I0
I lt numbers.size() 1) numbersI 0
for (int j0 j lt totalNumbers j)
numbersrand DIFFERENT_NUMBERS 1
for (int k1 k lt DIFFERENT_NUMBERS k)
cout ltlt k ltlt occurs ltlt numbersk ltlt
times ltlt \n return 0
9
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com