Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

1 COP 3330 Object-oriented Programming in C++
Arrays and Classes, Vectors Spring 2019

2 Arrays of Objects Array is an indexed collection of data elements of the same type Index is between 0 and size – 1 int myarray[60]; We can have an array of objects Examples Circle circles[30]; // array of 30 Circle objects Fraction fracs[54]; // an array of 54 Fraction objects Timer timers[10]; // an array of 10 objects of type Timer In an array of objects, each array position is a single object circles has 30 objects named circles[0], circles[1] … circles[29]

3 Initialization Use constructors to initialize objects in an array
The default constructor (if available) is invoked for each object in an array Fraction numList[4]; // 4 fractions using default constructor With different constructors, which constructor is invoked for each object in an array? Use initializer set Fraction numList[3] = {Fraction(2,4), Fraction(5), Fraction()}; // allocates an array of 3 fractions, initialized to 2/4, 5/1, and 0/1

4 Using Arrays of Objects
Indexing is the same as in regular arrays Dot-operator works the same as with single names objectName.memberName Name of each object is arrayName[index] Examples: Fraction rationals[20]; // create an array of 20 Fraction objects rationals[2].show(); // displays the third Fraction object rationals[6].input(); // calls Input function for the 7th Fraction cout << rationals[18].evaluate();

5 Arrays as Class Member Variables
C-style arrays are pretty primitive No boundary checks; unsafe Array can be used as a member data of a class Member functions can error check boundary conditions A good way to create safer array types Code review: dlist An array of floating-point numbers current: the number of numbers in the array (no greater than MAX) 5.4 2.3 10.6 1 2 3 4 5 6 7 8 9

6 Drawbacks of Arrays The size of the array must be a fixed constant
NOT a variable that you compute or read in from input stream Must make the array large enough to hold whatever amount of data conceivably need Passing an array as a parameter to a function is inconvenient Must also pass the size of the array as a separate parameter Cannot return an array from a function

7 Vector A vector is a sequence of elements of the same type
The size of a vector does not have to be a fixed constant It can grow or shrink during execution You can keep adding new elements WITHOUT having to know what the largest amount would be A vector always knows its own size Passing one to a function does not require pass the size Vectors can be returned from a function easily #include <vector> // you must include this header using namespace std; // vectors live in the std namespace

8 Declaring a Vector vector<type>
Template in C++: takes type as parameters Considerably more convenient than writing an explicit for-loop that initializes every element in the vector

9 Vector size() method reports the number of elements currently in the vector Accessing elements by their position in a vector uses exactly the same syntax as accessing elements of an array Indexing vector locations beyond the bounds of the vector can lead to the same memory errors as in arrays

10 Vector resize(int N) clear()
Resizes the vector so that it contains exactly N elements If it previously had less than N elements (thus making it larger), all of the old elements will remain as they were If it previously had more than N elements (thus shrinking it), any elements at index N or greater will be removed. clear() Removes all the elements from the vector and sets its size to 0

11 Vector push_back(Type element)
Adds an element to the end of the vector, increasing its size by 1

12 Vector Vectors can take almost any type as a parameter
e.g., multidimensional arrays

13 Vector vector< vector<int>> A(M, vector<int>(N));
A is a vector with M elements, whose elements are themselves vectors of N integers

14 Vector vector< vector<int>> A(M);
A vector that contained M empty vectors You could easily change the size of a single row, separately

15 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google