Object-Oriented Programming (OOP)





Example: Enhanced Arrays

Suppose that we want arrays with additional features:





Procedural Solution: make an ADT

typedef struct CIntArrayTag { int *data ; unsigned int size ; } CIntArray ;

What functions should we define to support this ADT?





Object-Oriented Solution: Vectors

vector<int> A(10) ; // array of 10 int, init value 0 vector<int> A(10,5) ; // array of 10 int, init value 5 vector<float> B ; // empty array of type float A[0] = 17 ; // first item of array A[9] = 18 ; // last item of the array A.at(i) ; // ith item with range check A.push_back(4) ; // add 11th item vector<int> C ; // another array of int C = A ; // copy item by item



More Vector Methods

A.size() ; // returns number of items A.resize(s) ; // shrink/grow to size s A.empty() ; // true if vector is empty A.clear() ; // makes vector empty A.reserve(s) ; // reserve memory for s items A.capacity() ; // memory reserved for this many items



Vectors & C++





C++ != OOP