1     //Static variables

 

//this goes into the header file

 

class twoNums

{

public:

      static int count;

      void setFirst(int x);

      //function to set the value of first

      //Postcondition:  first = x

      void printAll() const;

      //function outputs first, second and count

      static void increment();

      //function increments second

      //Postcondition:  second = second + 1

      twoNums();

      twoNums(int x);

      ~twoNums();

private:

      int first;

      static int second;

};

 

//in the main .cpp file

#include<iostream>

#include "twoNum.h"

using namespace std;

 

int main()

{

      twoNums object1(3), object2;

 

      object2.setFirst(5);

      twoNums::increment();

      twoNums::increment();

      twoNums::count++;

      object1.printAll();

      object1.increment();

      object2.increment();

      object1.printAll();

      object2.printAll();

 

}

//in the implementation .cpp file

 

int twoNums::count = 0;

#include<iostream>

#include "twoNum.h"

using namespace std;

int twoNums::count = 0;

int twoNums::second = 0;

void twoNums::setFirst(int x)

{

      first = x;

}

void twoNums::printAll() const

{

      cout << "First: " << first << "   Second: " << second <<                      "    Count: "<< count << endl;

}

 

void twoNums::increment()

{

      second++;

}

     

twoNums::twoNums()

{

      first = 0; second = 0;

}

twoNums::twoNums(int x)

{

      first = x;

      second = 0;

}

twoNums::~twoNums()

{

}

 

 

    2     //Template larger from Chapter 15 in Malik TextBook

 

#include <iostream>

#include "myString.h"

 

using namespace std;

 

template <class Type>  //instead of word class, you can use typename

Type larger(Type x, Type y);

 

int main()

{

    cout << "Line 1: Larger of 5 and 6 = "

         << larger(5, 6) << endl;                   //Line 1

    cout << "Line 2: Larger of A and B = "

         << larger('A', 'B') << endl;               //Line 2

    cout << "Line 3: Larger of 5.6 and 3.2 = "

         << larger(5.6, 3.2) << endl;               //Line 3

 

    newString str1 = "Hello";                       //Line 4

    newString str2 = "Happy";                       //Line 5

 

    cout << "Line 6: Larger of " << str1 << " and "

         << str2 << " = " << larger(str1, str2)

         << endl;                                   //Line 6

 

    return 0;

}

 

//class template Example from: http://www.cplusplus.com/doc/tutorial/templates/

 

template <class Type>

Type larger(Type x, Type y)

{

    if (x >= y)

        return x;

    else

        return y;

}

 

 

// second example of class templates
#include <iostream>
using namespace std;
 
template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};
 
template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}
 
int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}