
lafore_robert_objectoriented_programming_in_c
.pdf

Appendix G
966
////////////////////////////////////////////////////////////////
template <class atype> |
//function template |
atype avg(atype* array, int size) |
|
{ |
|
atype total = 0; |
|
for(int j=0; j<size; j++) |
//average the array |
total += array[j]; |
|
return (atype)total/size; |
|
} |
|
////////////////////////////////////////////////////////////////
int intArray[] = |
{1, 3, 5, |
9, 11, 13}; |
|
long longArray[] = |
{1, 3, 5, |
9, 11, 13}; |
|
double doubleArray[] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0}; |
|||
char charArray[] = |
{1, 3, 5, |
9, 11, 13}; |
|
int main() |
|
|
|
{ |
|
|
|
cout << |
“\navg(intArray)=” << |
avg(intArray, 6); |
|
cout << |
“\navg(longArray)=” << avg(longArray, 6); |
||
cout << |
“\navg(doubleArray)=” |
<< avg(doubleArray, 6); |
|
cout << |
“\navg(charArray)=” << (int)avg(charArray, 6) << endl; |
return 0;
}
2.
//ex14_2.cpp
//implements queue class as a template #include <iostream>
using namespace std; const int MAX = 3;
////////////////////////////////////////////////////////////////
template <class Type> class Queue
{
private: |
|
|
Type qu[MAX]; |
//array of any type |
|
int head; |
//index of start of queue (remove item here) |
|
int tail; |
//index of end of queue (insert item here) |
|
public: |
|
|
Queue() |
|
//constructor |
{ head = -1; tail = -1; } |
||
void put(Type |
var) |
//insert item at queue tail |
{ |
|
|
qu[++tail] |
= var; |
|
if(tail >=MAX-1) |
//wrap around if past array end |
|
tail = -1; |
|


Appendix G
968
////////////////////////////////////////////////////////////////
template <class Type> |
|
|
|
class Queue |
|
|
|
{ |
|
|
|
private: |
|
|
|
Type qu[MAX]; |
//array of any type |
||
int head; |
//index of front of queue (remove old item) |
||
int tail; |
//index of back of queue (insert new item) |
||
int count; |
//number of items in queue |
||
public: |
|
|
|
class full { |
}; |
//exception classes |
|
class empty |
{ }; |
|
|
//-------------------------------------------------------------- |
|
|
|
Queue() |
|
//constructor |
|
{ head = -1; tail = -1; |
count = 0; } |
||
void put(Type var) |
//insert item at queue tail |
||
{ |
|
|
|
if(count >= MAX) |
//if queue already full, |
||
throw full(); |
// |
throw exception |
|
qu[++tail] = var; |
//store item |
||
++count; |
|
|
|
if(tail >=MAX-1) |
//wrap around if past array end |
||
tail = -1; |
|
|
|
} |
|
|
|
//-------------------------------------------------------------- |
|
|
|
Type get() |
|
//remove item from queue head |
|
{ |
|
|
|
if(count <= 0) |
//if queue empty, |
||
throw empty(); |
// |
throw exception |
|
Type temp = qu[++head]; //get item |
|||
--count; |
|
|
|
if(head >= MAX-1) |
//wrap around if past array end |
||
head = -1; |
|
|
|
return temp; |
//return item |
||
} |
|
|
|
}; |
|
|
|
////////////////////////////////////////////////////////////////
int main() |
|
{ |
|
Queue<float> q1; |
//q1 is object of class Queue<float> |
float data; |
//data item obtained from user |
char choice = ‘p’; |
//’x’, ‘p’ or ‘g’ |
do |
//do loop (enter ‘x’ to quit) |
{ |
|


Appendix G
970
13.duplicate
14.b, c
15.points to
16.false
17.bidirectional
18.*iter++
19.d
20.c
21.true
22.iterators
23.it’s a string used to separate the printed values
24.b
25.the elements will be ordered
26.true
27.pairs (or associations)
28.false
29.a, d
30.constructor
Solutions to Exercises
1.
//ex15_1.cpp
//type float stored in array, sorted by sort() #include <iostream>
#include <algorithm> using namespace std;
int main()
{
int j=0, k; char ch;
float fpn, farr[100];
do {
cout << “Enter a floating point number: “; cin >> fpn;
farr[j++] = fpn;


Appendix G
972
int main()
{
int j;
list<int> theList; list<int>::iterator iter1; list<int>::iterator iter2;
for(j=2; j<16; j+=2) |
//fill list with 2, 4, 6, ... |
theList.push_back(j); |
|
cout << “Before reversal: “; |
//display list |
for(iter1=theList.begin(); iter1 != theList.end(); iter1++)
cout << *iter1 << “ “; |
|
iter1 = theList.begin(); |
//set to first element |
iter2 = theList.end(); |
//set to one-past-last |
--iter2; |
//move to last |
while(iter1 != iter2) |
|
{ |
|
swap(*iter1, *iter2); |
//swap front and back |
++iter1; |
//increment front |
if(iter1==iter2) |
//if even number of elements |
break; |
|
--iter2; |
//decrement back |
} |
|
cout << “\nAfter reversal: “; //display list for(iter1=theList.begin(); iter1 != theList.end(); iter1++)
cout << *iter1 << “ “; cout << endl;
return 0;
}
4.
//ex15_4.cpp
//a multiset automatically sorts person objects stored by pointer #include <iostream>
#include <set>
#pragma warning (disable:4786) #include <string>
using namespace std;
class person
{


Appendix G
974
//make persons
person* ptrP1 = new person(“KuangThu”, “Bruce”, 4157300); person* ptrP2 = new person(“McDonald”, “Stacey”, 3327563); person* ptrP3 = new person(“Deauville”, “William”, 8435150); person* ptrP4 = new person(“Wellington”, “John”, 9207404); person* ptrP5 = new person(“Bartoski”, “Peter”, 6946473); person* ptrP6 = new person(“McDonald”, “Amanda”, 8435150); person* ptrP7 = new person(“Fredericks”, “Roger”, 7049982); person* ptrP8 = new person(“McDonald”, “Stacey”, 7764987);
setPtrsPers.insert(ptrP1); |
//put persons in multiset |
setPtrsPers.insert(ptrP2); |
|
setPtrsPers.insert(ptrP3); |
|
setPtrsPers.insert(ptrP4); |
|
setPtrsPers.insert(ptrP5); |
|
setPtrsPers.insert(ptrP6); |
|
setPtrsPers.insert(ptrP7); |
|
setPtrsPers.insert(ptrP8); |
|
//display multiset cout << “\n\nSet sorted when created:”;
for(iter=setPtrsPers.begin(); iter != setPtrsPers.end(); iter++ ) (**iter).display();
iter |
= |
setPtrsPers.begin(); |
//delete all persons |
while( |
iter != setPtrsPers.end() ) |
|
|
{ |
|
|
|
delete *iter; |
//delete person |
||
setPtrsPers.erase(iter++); |
//remove pointer |
||
} |
|
|
|
cout |
<< endl; |
|
|
return |
0; |
|
}// end main()
Chapter 16
Answers to Questions
1.false
2.c, d
3.task
4.true
5.columns
6.a, c