1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
#include <iostream>
#include <cstdlib>
using namespace std;
//
template <class Type> class q_demo;
//
class project_engineer{
string first_name;
string last_name;
string project;
int id;
public:
void set_engineer(string,string,string,int);
void get_engineer(string*,string*,string*,int*);
project_engineer(string init_first = "John",
string init_last = "Smith",
string init_project = "Tahoe",
int init_id = 7)
{
first_name = init_first;
last_name = init_last;
project = init_project;
id = init_id;
}
void show_engineer();
};
//
void project_engineer::set_engineer(string first ,string last ,
string proj_name ,int engin_id )
{
first_name = first;
last_name = last;
id = engin_id;
}
//
void project_engineer::show_engineer()
{
cout << "Hello project_engineer \n" << "\n";
cout << first_name << " " <<
last_name << " " <<
project << " " <<
id;
}
//*******************************
//**** end class projec_engineer
//*******************************
//
//*******************************
//**** begin class q_object
//*******************************
template <class Type>
class q_object{ // p815 5th class QueueItem
public:
friend class q_demo<Type>;
q_object( const Type&);
Type get_object()
{
return object;
}
//
q_object* get_next()
{
return next;
}
private:
Type object;
q_object *next;
};
// **
// The execution of a constructor is done in two phases
// initialization and assignment. The {..} contain the assignment
// when it is null {} there is no assignment phase.
//
//
template <class Type>
q_object<Type>::q_object(const Type &t) : object(t)
{next = 0;}
//
//*****************************************
// end q_object **
//*****************************************
//*****************************************
//*** begin q_demo
//***
//***
//
//
template <class Type>
class q_demo{
private:
q_object<Type> *front;
q_object<Type> *back;
public:
q_demo() {front = 0; back = 0;}
~q_demo();
//
q_object<Type>* get_front()
{
return front;
}
void remove(bool&);
void add(const Type&);
bool is_empty() {
return front==0 ? true : false;}
};
//
template <class Type>
void q_demo<Type>::remove(bool& fail)
{
fail = false;
if (is_empty() == true)
{
fail = true;
cout << "ERR remove() on an empty queue \n";
}
if (is_empty() == false)
{
q_object<Type> *pt = front;
front = front->next;
//Type retval = pt->q_object;
delete pt;
//return retval;
}
}
//
template <class Type>
q_demo<Type>::~q_demo()
{
while (! is_empty() == false)
remove();
cout << "destructor q_demo \n";
}
//
template <class Type>
void q_demo<Type>::add(const Type &ob_val)
{
//cout << "Hello add \n";
q_object<Type> *pt = new q_object<Type>(ob_val);
if (is_empty())
{
front = pt;
back = pt;
}
else
{
back->next = pt;
back = pt;
}
}
//
//
int main ()
{
project_engineer ob1_prj_eng("Jane", "Smith", "Solano", 96);
q_object<project_engineer> ob1_q_object(ob1_prj_eng);
//q_demo<project_engineer> ob2_q_demo();
//
cout << "Hello queue world \n";
//**************************************
q_demo<project_engineer> *p2q_demo = new q_demo<project_engineer>;
//
p2q_demo->add(ob1_prj_eng);
ob1_prj_eng.set_engineer("John ", "Lantz ", "Tahoe ", 3456);
p2q_demo->add(ob1_prj_eng);
ob1_prj_eng.set_engineer("Sally ","Brown ","Reno ", 1000);
p2q_demo->add(ob1_prj_eng);
ob1_prj_eng.set_engineer("Joan ","White ","Reno ", 1050);
p2q_demo->add(ob1_prj_eng);
ob1_prj_eng.set_engineer("Pauline ","Richard ","Redwood ", 99);
p2q_demo->add(ob1_prj_eng);
//
q_object<project_engineer> *p2front = p2q_demo->get_front();
project_engineer ob_temp = p2front->get_object();
ob_temp.show_engineer();
//*********************
q_object<project_engineer> *p2next = p2front->get_next();
ob_temp = p2next->get_object();
ob_temp.show_engineer();
//**********************
p2next = p2next->get_next();
ob_temp = p2next->get_object();
ob_temp.show_engineer();
//**********************
p2next = p2next->get_next();
if (p2next == 0)
{
cout << "next is 0 :: End of queue \n";
}
if (p2next != 0)
{
ob_temp = p2next->get_object();
ob_temp.show_engineer();
}
//**********************
p2next = p2next->get_next();
//**********************
if (p2next != 0)
{
ob_temp = p2next->get_object();
ob_temp.show_engineer();
}
if (p2next == 0)
{
cout << "End of queue \n";
}
//**********************
cout << "Calling remove \n";
bool fail=false;
p2q_demo->remove(fail);
p2front = p2q_demo->get_front();
ob_temp = p2front->get_object();
ob_temp.show_engineer();
system("pause");
return 0;
}
|