CSCI 207: Fundamentals of Data Structures & Algorithms: Review of Object Oriented Programming Basics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

CSCI 207: Fundamentals of Data Structures &

Algorithms

Lecture 3

Review of Object Oriented Programming Basics

1
Templates

 Templates are C++ mechanism for implementing both template functions and
generic classes. The C++ template function uses the template mechanism to create
a function whose parameter types are not fixed with the definition of the function.
They are instantiated at the function call.

template <class T> T max(const T&a, const T&b)


{
return ( a>b ? a : b);
}

 The template mechanism instantiates the parameter, in this case the class T,
when the class of function is called. For example, on encountering the call max(3,4),
the compiler instantiates T with type integer; explicitly call max<int>(3,4)

 Templates are type safe which means the compiler will validate types while
compiling, and throw an error if you try to assign the wrong type to a variable.

2
Generic Classes

 "A generic class is a class that serves as a template for other classes, in which the
template may be parameterised by other classes, objects and/or operations. A
generic class must be instantiated (its parameters filled in) before objects of that
class can be created”. Generic classes are typically used as container classes.

 A powerful feature of C++ is the possibility of declaring generic classes by using


type parameters in the class declaration. For example, if we need to declare a class
that uses an array for storing some items, then we may declare this class as:

class intClass {

int storage[50];

..................

};

3
Generic Classes
 However, in this way, we limit the usability of this class to integers only; if we
need a class that performs the same operations as intClass except that it operates
on float numbers, then a new declaration is needed, such as

class floatClass {
float storage[50];
..................
};

 If storage is to hold structures, or pointers to characters, then two more classes


must be declared.

4
Generic Classes
 It is much better to declare a generic class and decide what type of items the
object is referring to only when defining (creating or instantiating) the object.

 Fortunately, C++ allows us to declare a class in this way, and the declaration for
the example is
template<class genType>
class genClass {
genType storage[50];
.................
};

Later, we make the decision about how to initialize genType:

genClass<int> intObject;
genClass<float> floatObject;

 This generic class becomes a basis for generating two new classes, genClass
of int and genClass of float, and then these two classes are used to create two
objects: intObject and floatObject. 5
Generic Classes
 We can go even further than that by not committing ourselves to 50 cells in
storage and by delaying that decision until the object definition stage. But just in
case, we may leave a default value so that the class declaration is now:

template<class genType, int size = 50>


class genClass {
genType storage[size];
..................
};

 The object definition is now:

genClass<int> intObject1; // use the default size;


genClass<int,100> intObject2;
genClass<float,123> floatObject;

6
The Standard Template Library (STL)

 C++ is an object-oriented language, but recent extensions to the language bring


C++ to a higher level. The most significant addition to the language is the Standard
Template Library (STL).

 The library includes three types of generic entities:


 Containers
 Iterators
 Algorithms

 Algorithms are frequently used functions that can be applied to different data
structures. The application is mediated through the iterators that determine which
algorithms can be applied to which types of objects.

 The STL relieves programmers from writing their own implementations of various
classes and functions. Instead, they can use pre-packaged generic (general)
implementations accommodated to the problem at hand.

7
Containers

 A container is a data structure that holds some objects that are usually of the
same type. Different types of containers organize the objects within them
differently.

 The STL includes the following containers: deque, list, map,


multimap, set, multiset, stack, queue, priority_queue, and vector

 The STL containers are implemented as template classes that include member
functions specifying what operations can be performed on the elements stored
in the data structure specified by the container or on the data structure (container)
itself.

 Some operations can be found in all containers, although they may be


implemented differently.

8
Containers

 The member functions common to all containers include the default constructor,
copy constructor, destructor, empty(), max_size(), size(), swap(), operator=, as
well as other overloaded relational operator functions for some containers.

 Elements stored in containers can be of any type (int, float, CStudent, .etc.), and
they have to supply at least a default constructor, a destructor, and an assignment
operator.

 This is particularly important for user-defined types.

 Also, a copy constructor and the function operator = should be provided if data
members are pointers (why?).

9
Iterators

 An iterator is an object used to reference an element stored in a container. Thus,


it is a generalization of the pointer (like a pointer).

 An iterator allows for accessing information included in a container so that


desired operations can be performed on these elements.

 As a generalization of pointers, iterators retain the same dereferencing notation.


For example, *i is an element referenced by iterator i.

 However, all iterators do not have similar functionality as that of pointers. That is
different containers support different iterators.

10
Iterators

 No iterators are supported for the stack, queue, and priority_queue containers.
Iterator operations for classes list, map, multimap, set, and multiset are as
follows (i1 and i2 are iterators, n is a number):

i1++, ++i1, i1--, --i1


i1 = i2
i1 == i2, i1 != i2
*i1

In addition to these operations, iterator operations for classes deque and


vector are as follows:

i1 < i2, i1 <= i2, i1 > i2, i1 >= i2


i1 + n, i1 - n
i1 += n, i1 -= n
i1[n]

11
Algorithms
 The STL provides some 70 generic functions, called algorithms, that can be
applied to the STL containers and to arrays.

 These algorithms are implementing operations that are very frequently used in
most programs, such as locating an element in a container, inserting an element into
a sequence of elements, removing an element from a sequence, modifying
elements, comparing elements, and so on.

 Almost all STL algorithms use iterators to indicate the range of elements
on which they operate. For example,
i3 = find(i1, i2, el);

returns an iterator indicating the position of element el in the range i1 up to, but
not including i2.

n = count_if(i1, i2, oddNum);

counts with the algorithm count_if() the elements in the range indicated by
iterators i1 and i2 for which a one-argument user-defined Boolean function
oddNum() returns true.
12
Algorithms
 Fill and Generate:

fill(iterator1, iterator2, value);


fills the values of the elements between iterator1 and iterator2 with value.

generate(iterator1, iterator2, function);


similar to fill except that it calls a function to return value.

 Comparing sequences of values:

bool equal(iterator1, iterator2, iterator3);


compares sequence from iterator1 to iterator2 with the sequence beginning at
iterator3. return true is they are equal, false otherwise.

13
Vectors in the Standard Template Library
 The simplest STL container is the vector, which is a data structure with
contiguous blocks of memory just like an array.

 Because memory locations are contiguous, they can be randomly accessed so


that the access time of any element of the vector is constant.

 Storage is managed automatically so that on an attempt to insert an element


into a full vector, a larger memory block is allocated for the vector, the vector
elements are copied to the new block, and the old block is released.

 A vector is thus a flexible array; that is, an array whose size can be dynamically
changed.

 Example of vectors in the STL is given in the practical lecture.

14
Thank You

Questions?

15

You might also like