In File Driver.cpp

#include "things.h"

void main()

{

try {

Widgets<int> nW;

ReallySpecialWidgets<double> dW;

for(int i=0;i<10;i++) {

nW.Add( 10 - 2 * i );

dW.Add( 10 - i );

}

nW.Sort();

dW.Sort();

for(i=0;i<10;i++) {

nW.Display(cout,i);

dW.Display(cout,i);

cout < endl;

}

dW.Add( 45.0 );

}

catch (int) {

cout < "Bigtime Error" < endl;

}

catch (char c ) {

cout < "Little Error " < c < endl;

}

catch (double z ) {

cout < z < endl;

}

catch (...) {

cout < "So what" < endl;

}

}

In File Things.h

#ifndef _thingsh

#define _thingsh

#include <iostream.h>

class Thingy {

public:

Thingy(int n = 100) { m_nMax = n; };

virtual ~Thingy() {};

virtual void Sort() const = 0;

protected:

intm_nMax;

};

template<class IType>

class Widgets : public Thingy {

public:

Widgets(int = 10);

Widgets(const Widgets &);

~Widgets() { delete [] m_pitA; };

void Sort() const;

void Add( const IType & );

void Display( ostream &, int );

protected:

IType*m_pitA;

intm_nNumber;

};

template<class ZZZ>

class ReallySpecialWidgets : public Widgets<ZZZ> {

public:

ReallySpecialWidgets() : Widgets<ZZZ>() {};

ReallySpecialWidgets(const ReallySpecialWidgets & w)

: Widgets<ZZZ>(w) {};

void Sort() const {};

};

template<class A>

Widgets<A>::Widgets(int n) throw(int) : Thingy(n)

{

m_nNumber = 0;

m_pitA = new A[n];

if ( !m_pitA ) throw 99;

}

template<class Z>

Widgets<Z>::Widgets(const Widgets & w) throw(char) : Thingy(w.m_nMax)

{

m_nNumber = w.m_nMax;

m_pitA = new Z[m_nNumber];

if ( !m_pitA ) throw 'z';

for(int i=0;i<m_nNumber;i++) m_pitA[i] = w.m_pitA[i];

}

template<class B>

void Widgets<B>::Add(const B & a) throw(double)

{

if ( m_nNumber == m_nMax) throw 99.;

m_pitA[m_nNumber++] = a;

}

template<class C>

void Widgets<C>::Sort() const

{

for(int i=0;i<m_nNumber-1; i++) {

for(int j=i+1;j<m_nNumber;j++) {

if ( m_pitA[i] > m_pitA[j] ) {

C Temp = m_pitA[i];

m_pitA[i] = m_pitA[j];

m_pitA[j] = Temp;

}

}

}

}

template<class C>

void Widgets<C>::Display(ostream & o, int i) throw(long)

{

if ( i < 0 || i >= m_nNumber) throw 99L;

o < m_pitA[i] < " ";

}

#endif

CGS 6305

Fall, 1998 November 18, 1998

Exam 3

Name: ______

Instructions:

You have one hour to answer the following questions. You may NOT use YOUR book or YOUR notes, etc. Answer the questions in the spaces provided. Make sure your answers are clearly indicated.

Special Becky notice: Look on the back sides of handouts.

Good luck!

Use the handout to answer questions 1-8. Study the program carefully before answering these questions.

1. (20 points) What is the output of the driver program?

  1. (10 points) Explain what happens when the following line is compiled.

ReallySpecialWidgets<double> dW;

  1. (5 points) What member function gets executed with

nW.Sort();

  1. (5 points) What member function gets executed with

dW.Sort();

5. (5 points) What member functions get executed with the following code:

Thingy T;

T.Sort();

6. (5 points) Suppose this code is added after the definition of dW.

Thingy *pT;

pT = &dW;

What member functions get executed with the following code:

pT->Add( 2 );

pT->Sort();

7. (5 points) What problem (if any) would arise with:

Widgets<char *> pchA;

pchA.Add( “Hi There”);

pchA.Add( “Ho There”);

pchA.Add( “There”);

pchA.Sort();

  1. (5 points) What problem (if any) would arise with:

Widgets<char> chA;

chA.Add( ‘a’ );

chA.Add( ‘z’ );

chA.Add( ‘a’ );

chA.Sort( );

  1. (5 points) What would happen if the first for loop in the driver program had <=10 replace <10?
  1. (5 points) Which catch catches a throw from class Widgets Display member functions?

11. (5 points) What code in Things.h are currently ignored by the Visual C++ compiler?

12. (10 points) Give definitions for:

a) Queue

b) Standard template library

c) Tree

d) Stack

e) What is the difference between a deque and a queue?

13. (6 points) What are the three main components of STL?

14. (9 points) What are the three container types in STL? Give a description of each.