Arrays and Classes Programming in C++ Fall 2008

Slides:



Advertisements
Similar presentations
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
1 Lab Session-IX CSIT121 Fall 2000 w Arrays and Their Usage w Examples and Lab Exercises w Passing Arrays to Functions w Examples and Exercises w Sorting.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ARRAYS.
C++ Lesson 1.
Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Arrays Low level collections.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Array, Strings and Vectors
Andy Wang Object Oriented Programming in C++ COP 3330
Arrays Declarations CSCI N305
Andy Wang Object Oriented Programming in C++ COP 3330
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
New Structure Recall “average.cpp” program
Object-Oriented Programming Using C++
More on Classes Programming in C++ Fall 2008
Andy Wang Object Oriented Programming in C++ COP 3330
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
Advanced Program Design with C++
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Classes and Objects.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Exceptions 1 CMSC 202.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
7 Arrays.
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
Arrays Arrays A few types Structures of related data items
Submitted By : Veenu Saini Lecturer (IT)
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
COP 3330 Object-oriented Programming in C++
Introduction to Programming
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Class rational part2.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Array What it is. How to use it How to declare it How to initialize it.
Pointers, Dynamic Data, and Reference Types
Chapter 11 Classes.
CMSC 202 Lesson 20 Exceptions 1.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays and Classes Programming in C++ Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

Arrays An array is a data structure comprised of a consecutive group of memory locations that all have the same name and same type Arrays are static in that they remain the same size during program execution You can allocate new arrays You can de-allocate arrays Array element are numbered beginning with zero [0]. To reference a particular location in an array, you specify the name of the array and the position number of the element you want.

Arrays Example: int c[6] = { -45, 72, 34, -87, 6, 35 } ; // So, here are the stored values: c[0] = -45 c[1] = 72 c[2] = 34 c[3] = -87 c[4] = 6 c[5] = 35

Arrays To obtain the value of a particular element, you have to use a subscript by placing [ ] and the subscript value after the element name. Examples: c[0+index]= 456; c[5] = -44; c[a+b] = 200; c[a] = c[a+1];

Arrays Declaring Arrays You must specify the type, then the name, and then number elements of that type you want declared. float b[3], c[16], d[25], e[45]; Two ways to initialize: Loop Structure: for (int i=0; i<16; i++) c[i]=0; In declaration float b[6] ={0.0, 0.0, 0.0}; Implicit float e[45] = {1.0}; // Initializes the first one to 1.0 and the rest to 0.0 You can put the initialization in without the size int c[] = {1, 2, 3, 4, 5] The following will result in a compiler error: int c[];

Arrays Sound programming practices: Always declare a constant value to size your array: const int arraysize = 10; int c[arraysize] for (int=1; int<arraysize; i++) Why: If you have to change the size of the array, you need only do it on one location.

Arrays Sound programming practices: Declare one more element in an array than you need. Used to exchange two elements. a[10] = a[0]; a[0] = a[1]; a[1]= a[10]; Boundary protection C and C++ do not check array boundaries. Trying to access or change an element out of bounds may or may not cause an abnormal termination but it will cause problems.

Arrays and Classes // frac.h -- Fraction class declaration // Bob Myers class Fraction { friend Fraction operator+(const Fraction& f1, const Fraction& f2); public: Fraction(); Fraction(int n, int d=1); void Input(); viod Show() const; int GetNumerator() const; int GetDenominator() const; bool SetValue(int n, int d=1); double Evaluate() const; privat: int numerator; int denominator; };

Arrays and Classes Fraction rationals[20]; Fraction numList[3] = { Fraction(2,4), Fraction(5), Fraction()}; rationals[5].Show(); rationals[6].Input cout << rationals[18].Evaluate();

Arrays and Classes Arrays inside member functions Remember that C style arrays are very primitive and you can exceed boundaries. Inside the member function you can add error checking. Also, you can redefine - - and ++. Common implementation is to have a private data item called “current” which points to the current array item. If any operation tries to move “current” outside the boudaries of the list, an error is handled.