Docsity

Templated Classes and Vectors - Lecture Notes | CSCI 1200, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Computer Science II — CSci 1200
Lecture 18
Templated Classes and Vectors
Announcement — Two Weeks Before Thanksgiving
•Today’s lecture will cover the implementation of vectors.
•The scheduled material on operators is covered instead in a separate
handout.
–You will not be tested directly on this material.
–You may find some of it useful for HW 7, especially the input
and output stream operators.
–Some material on operators will be worked into other lecture
notes — you will be responsible for this material.
•Test 3 review material will be available on-line by Friday, November
11.
•Test 3 is Friday, November 18.
•Homework 7 is due Tuesday, November 22.
•There will be NO CLASS on Tuesday, November 22.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Templated Classes and Vectors - Lecture Notes | CSCI 1200 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Lecture 18

Templated Classes and Vectors

Announcement — Two Weeks Before Thanksgiving

  • Today’s lecture will cover the implementation of vectors.
  • The scheduled material on operators is covered instead in a separate handout. - You will not be tested directly on this material. - You may find some of it useful for HW 7, especially the input and output stream operators. - Some material on operators will be worked into other lecture notes — you will be responsible for this material.
  • Test 3 review material will be available on-line by Friday, November
  • Test 3 is Friday, November 18.
  • Homework 7 is due Tuesday, November 22.
  • There will be NO CLASS on Tuesday, November 22.

Review from Lecture 17

  • Arrays and pointers
  • Different types of memory
  • Dynamic allocation of arrays

Today’s Lecture

  • Designing our own container classes
  • Vectors
  • Templated classes
  • Copy constructors, assignment operators and destructors
  • Implementation

Designing Our Own Containers

  • Mimic the interface of standard library containers
  • Study the design of memory management code and iterators.
  • Move toward eventually designing our own, more sophisticated classes.
  • In the actual text of the code files, templated member functions are often defined (written) inside the class declaration.
  • The templated functions defined outside the template class declaration must be preceeded by the phrase

template

and then when Vec is referred to it must be

Vec

  • Therefore for member function create (two versions), we have:

template void Vec::create

Syntax and Compilation

  • Templated classes and templated member functions are not created (compiled) until they are needed.
  • Compilation of the class declaration is triggered by a line of the form

Vec v1;

with int replacing T. This also compiles the default constructor for Vec — because it is used here.

  • Other member functions are not compiled unless they are used.
  • When a different type is used with Vec, for example in the declaration

Vec z;

the template class declaration is compiled again, this time with double replacing T instead of int. Again, however, only the member functions used are compiled.

  • This is very different from ordinary classes, which are usually compiled separately and all functions are compiled regardless of whether or not they are needed.
  • The code of templated classes AND the member functions must be available where they are used.
  • As a result, member functions definitions are often included in the class declaration or attached below but still in the .h file. If member function definitions are placed in a separate .cpp file, this file must be #included, just like the .h file, because the compiler needs to see it to generate code.

Member Variables

Now, looking inside the Vec class at the member variables:

  • m data is a pointer to the start of the array (after it is allocated
    • Recall the (near) equivalence between pointers and arrays.
  • m size indiciates the number of locations currently in use in the vector (as indicated by the size() member function),
  • m alloc is the number of entries in the dynamically allocated block of memory (the actually-allocated array!)

Drawing a picture, which we will do in class, will help clarify this, especially the distinction between m size and m alloc.

Typedefs, Iterators and Pointers

  • A number of types are created through typedef statements in the first public area of Vec.
  • Once created the names are used as ordinary class type names. There- fore,

Vec::iterator

is an iterator type defined by the Vec class. It is just a T *.

  • Also,

Vec::size_type

is the size type, defined here as an unsigned int.

template Vec :: Vec( const Vec& v ) : m_data(v.m_data), m_size(v.m_size), m_alloc(v.m_alloc) {}

In other words, it would construct each member variable from the corresponding member variable of v.

  • This can be dangerous, as the following exercise illustrates.

Exercise

Suppose we used the default version of the assignment operator and copy constructor in our Vec class. What would be the output of the following program? Assume all of the operations except the copy constructor behave as they would with a std::vector.

Vec v(4, 0.0); v[0] = 13.1; v[2] = 3.14; Vec u(v); u[2] = 6.5; u[3] = -4.8; for ( unsigned int i=0; i<4; ++i ) cout << u[i] << " " << v[i] << endl;

Explain why this happens by drawing a picture of the memory of both u and v.

Exercise

Write the private member function copy.

Aside (1): the “this” pointer

  • All class objects have a special pointer defined called this.
  • The this pointer simply points to the class object. It may not be changed.
  • The expression *this is a reference to the class object.
  • The this point is used in several ways:
    • Make it clear when member variables of the current object are being used
    • Check to see when an assignment is self-referencing.
    • Return a reference to the current object.

Aside (2): Assignment operators, generally speaking

  • Assignment operators of the form

v1 = v2;

are translated by the compiler as

v1.operator=(v2);

  • Cascaded assignment operators of the form

v1 = v2 = v3;

are translated by the compiler as

v1.operator=(v2.operator=(v3));

  • Therefore, the value of the assignment operator (v2 = v3) must be suitable for input to a second assignment operator. This in turn means the result of an assignment operator ought to be a reference to an object.

Assignment operator for Vec

The implementation of an assignment operator usually takes on a very spe- cific form, as illustrated for Vec:

  • Do real work if there is a self-assignment.
  • Otherwise, destroy the contents of the current object then copy the passed object.
  • Return a reference to the (copied) current object, using the this pointer.

Destructor

  • Called implicitly when an automatic object goes out of scope or a dynamic object is deleted. - It can never be called explicitly!
  • Must delete the dynamic memory owned by the class.
  • The syntax of the function definition is a bit weird.
    • The ~ has been used as a logic negation in other contexts.

Increasing the Size of the Vec

push_back( T const& x )

  • Adds to the end of the array, increasing data_end by one T location.
  • But wait, what if m_size == m_alloc already, which means the allo- cated array is full?
  • We need to increase the size of the array. Therefore we need to
    1. Allocate a new, larger array. The best strategy is generally to double the size of the current array.
    2. If the array size was originally 0, doubling does nothing. We must be sure that the resulting size is at least 1.
    3. Then we need to copy the contents of the current array.
    4. Finally, we must delete current array, make the data_start pointer point to the start of the new array, and adjust the other two pointers