Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Classes Programming in C++ Fall 2008

Similar presentations


Presentation on theme: "Arrays and Classes Programming in C++ Fall 2008"— Presentation transcript:

1 Arrays and Classes Programming in C++ Fall 2008
Dr. David A. Gaitros

2 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.

3 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

4 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];

5 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[];

6 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.

7 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.

8 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; };

9 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();

10 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.


Download ppt "Arrays and Classes Programming in C++ Fall 2008"

Similar presentations


Ads by Google