Lab 03

typedef

Format of a typedef statement:

  
     typedef _DataType_ _AliasName_;
     
  

DataType can be any of the built-in data types in C++. So for example, it could be integer or double. Could also be any class name that you’ve created.

AliasName is what you want the new name to be.

typedef examples

  
    typedef double LargeNumber;
    // here LargeNumber could now be used
    //   to declare a variable of type double.
    
    LargeNumber aNumber;
    
    typedef double* PointerDouble;
    //now PointerDouble can be used to 
    //  declare a pointer to a double
    
    typedef int IntArray[15];
    //IntArray now can be used to create
    //  an array of integers of size fifteen.
  

Note the format of the typedef calls matches normal variable declaration. That is to say, without the typedef component each of these examples would create variables of the DataType type. However, instead of variable creation, with the typedef statement, new types are created instead. These new types can then be used as the prefix to the declaration of new variables.

more information about typedef

typedef

Format of a typedef statement:

typedef DataType AliasName;

DataType can be any of the built-in data types in C++. So for example, it could be integer or double.

AliasName is what you want the new name to be.

typedef examples

Note the format of the typedef calls matches normal variable declaration. That is to say, without the typedef component each of these examples would create variables of the DataType type. However, instead of variable creation, with the typedef statement, new types are created instead. These new types can then be used as the prefix to the declaration of new variables.

more information about typedef

See my example (mostly from the book) for more experience with pointers.

Using getline

  
    getline(in,a_string, '|')
    
  

Notes on insert method


  ListNode *newPtr = new ListNode;

Here we are dynamically creating a new ListNode object that will hold the string newItem. So newPtr is a ListNode pointer which points to (contains the address of) this new ListNode.


  newPtr->item = newItem;

Here we assign the item variable held inside the ListNode object pointed to by newPtr. This is special syntax needed when dealing with objects or structures referenced to by pointers instead of actual variables.

If this were a statically allocated ListNode, the equivalent call would be l_node1.item = newItem;, where l_node1 is an object of type ListNode. However, since it is dynamically allocated, we can’t use the [object] . [method] syntax, and instead must use the [pointer] [method] syntax.

  
    newPtr->next = head;
    
    head = newPtr;
  

Here we are dealing with the ‘head’ pointer. Remember, head is not an actual node, but just another node pointer, just like the ‘next’ pointer in each ListNode.

Catching bad allocations


  try {} catch (bad_alloc e) {}

Good example of multiple layers of exception handling. The ‘new’ function could fail if we run out of memory (although very unlikely for this lab). This shows the method ‘insert’ catching the possible exception that can occur (bad_alloc) and then throwing its own exception as a way of dealing with it (ListException). This allows our main function to only deal with list exceptions. It also abstracts the way we are holding the list from how it is accessed.