Return to the C++ Demos Home Page
SAMS Teach Yourself C++ in 21 Days
Chapter Nineteen
Prog 1 : Using Templates
#include <iostream>
const int DefaultSize = 10;
// declare a simple Animal class so that we can
// create an array of animals
class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight; }
void Display() const { std::cout << itsWeight; }
private:
int itsWeight;
};
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::Animal():
itsWeight(0)
{}
template <class T> // declare the template and the parameter
class Array // the class being parameterized
{
public:
// constructors
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }
// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const
{ return pType[offSet]; }
// accessors
int GetSize() const { return itsSize; }
private:
T *pType;
int itsSize;
};
// implementations follow...
// implement the Constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
pType = new T[size];
for (int i = 0; i<size; i++)
pType[i] = 0;
}
// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
}
// operator=
template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete [] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
return *this;
}
// driver program
int main()
{
Array<int> theArray; // an array of integers
Array<Animal> theZoo; // an array of Animals
Animal *pAnimal;
// fill the arrays
for (int i = 0; i < theArray.GetSize(); i++)
{
theArray[i] = i*2;
pAnimal = new Animal(i*3);
theZoo[i] = *pAnimal;
delete pAnimal;
}
// print the contents of the arrays
for (int j = 0; j < theArray.GetSize(); j++)
{
std::cout << "theArray[" << j << "]:\t";
std::cout << theArray[j] << "\t\t";
std::cout << "theZoo[" << j << "]:\t";
theZoo[j].Display();
std::cout << std::endl;
}
return 0;
}
Grabbed this code straight from the book...pg 633.
Prog 2 : Non Template Friend Function
#include <iostream>
using namespace std;
const int DefaultSize = 10;
// declare a simple Animal class so that we can
// create an array of animals
class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight; }
void Display() const { cout << itsWeight; }
private:
int itsWeight;
};
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::Animal():
itsWeight(0)
{}
template <class T> // declare the template and the parameter
class Array // the class being parameterized
{
public:
// constructors
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }
// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const
{ return pType[offSet]; }
// accessors
int GetSize() const { return itsSize; }
// friend function
friend void Intrude(Array<int>);
private:
T *pType;
int itsSize;
};
// friend function. Not a template, can only be used
// with int arrays! Intrudes into private data.
void Intrude(Array<int> theArray)
{
cout << "\n*** Intrude ***\n";
for (int i = 0; i < theArray.itsSize; i++)
cout << "i: " << theArray.pType[i] << endl;
cout << "\n";
}
// implementations follow...
// implement the Constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
pType = new T[size];
for (int i = 0; i<size; i++)
pType[i] = 0;
}
// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
}
// operator=
template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete [] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
return *this;
}
// driver program
int main()
{
Array<int> theArray; // an array of integers
Array<Animal> theZoo; // an array of Animals
Animal *pAnimal;
// fill the arrays
for (int i = 0; i < theArray.GetSize(); i++)
{
theArray[i] = i*2;
pAnimal = new Animal(i*3);
theZoo[i] = *pAnimal;
}
int j;
for (j = 0; j < theArray.GetSize(); j++)
{
cout << "theZoo[" << j << "]:\t";
theZoo[j].Display();
cout << endl;
}
cout << "Now use the friend function to ";
cout << "find the members of Array<int>";
Intrude(theArray);
cout << "\n\nDone.\n";
return 0;
}
Grabbed this code straight from the book...pg 637.
Prog 3 : Using the STD, specifically the sequence container vector...
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
public:
Student();
Student(const string& name, const int age);
Student(const Student& rhs);
~Student();
void SetName(const string& name);
string GetName() const;
void SetAge(const int age);
int GetAge() const;
Student& operator=(const Student& rhs);
private:
string itsName;
int itsAge;
};
Student::Student()
: itsName("New Student"), itsAge(16)
{}
Student::Student(const string& name, const int age)
: itsName(name), itsAge(age)
{}
Student::Student(const Student& rhs)
: itsName(rhs.GetName()), itsAge(rhs.GetAge())
{}
Student::~Student()
{}
void Student::SetName(const string& name)
{
itsName = name;
}
string Student::GetName() const
{
return itsName;
}
void Student::SetAge(const int age)
{
itsAge = age;
}
int Student::GetAge() const
{
return itsAge;
}
Student& Student::operator=(const Student& rhs)
{
itsName = rhs.GetName();
itsAge = rhs.GetAge();
return *this;
}
ostream& operator<<(ostream& os, const Student& rhs)
{
os << rhs.GetName() << " is " << rhs.GetAge() << " years old";
return os;
}
template<class T>
// display vector properties
void ShowVector(const vector<T>& v);
typedef vector<Student> SchoolClass;
int main()
{
Student Harry;
Student Sally("Sally", 15);
Student Bill("Bill", 17);
Student Peter("Peter", 16);
SchoolClass EmptyClass;
cout << "EmptyClass:\n";
ShowVector(EmptyClass);
SchoolClass GrowingClass(3);
cout << "GrowingClass(3):\n";
ShowVector(GrowingClass);
GrowingClass[0] = Harry;
GrowingClass[1] = Sally;
GrowingClass[2] = Bill;
cout << "GrowingClass(3) after assigning students:\n";
ShowVector(GrowingClass);
GrowingClass.push_back(Peter);
cout << "GrowingClass() after added 4th student:\n";
ShowVector(GrowingClass);
GrowingClass[0].SetName("Harry");
GrowingClass[0].SetAge(18);
cout << "GrowingClass() after Set\n:";
ShowVector(GrowingClass);
return 0;
}
//
// Display vector properties
//
template<class T>
void ShowVector(const vector<T>& v)
{
cout << "max_size() = " << v.max_size();
cout << "\tsize() = " << v.size();
cout << "\tcapacity() = " << v.capacity();
cout << "\t" << (v.empty()? "empty": "not empty");
cout << "\n";
for (int i = 0; i < v.size(); ++i)
cout << v[i] << "\n";
cout << endl;
}
Grabbed this code straight from the book...pg 661.
Prog 4 : More STD with the associative container map...
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Student
{
public:
Student();
Student(const string& name, const int age);
Student(const Student& rhs);
~Student();
void SetName(const string& name);
string GetName() const;
void SetAge(const int age);
int GetAge() const;
Student& operator=(const Student& rhs);
private:
string itsName;
int itsAge;
};
Student::Student()
: itsName("New Student"), itsAge(16)
{}
Student::Student(const string& name, const int age)
: itsName(name), itsAge(age)
{}
Student::Student(const Student& rhs)
: itsName(rhs.GetName()), itsAge(rhs.GetAge())
{}
Student::~Student()
{}
void Student::SetName(const string& name)
{
itsName = name;
}
string Student::GetName() const
{
return itsName;
}
void Student::SetAge(const int age)
{
itsAge = age;
}
int Student::GetAge() const
{
return itsAge;
}
Student& Student::operator=(const Student& rhs)
{
itsName = rhs.GetName();
itsAge = rhs.GetAge();
return *this;
}
ostream& operator<<(ostream& os, const Student& rhs)
{
os << rhs.GetName() << " is " << rhs.GetAge() << " years old";
return os;
}
template<class T, class A>
void ShowMap(const map<T, A>& v); // display map properties
typedef map<string, Student> SchoolClass;
int main()
{
Student Harry("Harry", 18);
Student Sally("Sally", 15);
Student Bill("Bill", 17);
Student Peter("Peter", 16);
SchoolClass MathClass;
MathClass[Harry.GetName()] = Harry;
MathClass[Sally.GetName()] = Sally;
MathClass[Bill.GetName()] = Bill;
MathClass[Peter.GetName()] = Peter;
cout << "MathClass:\n";
ShowMap(MathClass);
cout << "We know that " << MathClass["Bill"].GetName()
<< " is " << MathClass["Bill"].GetAge() << " years old\n";
return 0;
}
//
// Display map properties
//
template<class T, class A>
void ShowMap(const map<T, A>& v)
{
for (map<T, A>::const_iterator ci = v.begin();
ci != v.end(); ++ci)
cout << ci->first << ": " << ci->second << "\n";
cout << endl;
}
Grabbed this code straight from the book...pg 669.
Prog 5 : Using Algorithm Classes and Function Objects
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
class Print
{
public:
void operator()(const T& t)
{
cout << t << " ";
}
};
int main()
{
Print<int> DoPrint;
vector<int> vInt(5);
for (int i = 0; i < 5; ++i)
vInt[i] = i * 3;
cout << "for_each()\n";
for_each(vInt.begin(), vInt.end(), DoPrint);
cout << "\n";
return 0;
}
Grabbed this code straight from the book...pg 674.