Hi,
I am new in implementing Templates.
My .h file.
class AnimalControl
{
public:
DogCatcher* getDogCatcherRef();
void disturbance();
void incarcerate(Animal* temp_ptr);
AnimalControl(); // constructor
~AnimalControl(); // destructor
private:
int startpos; //start point of incarcerate
int insertpos; //first free slot
DogCatcher* dogcthr;
Animal* ptr_animal[10];
};
Inside my .cpp file
AnimalControl::AnimalControl()
{
cout << "AnimalControl constructor: " << endl;
startpos=insertpos=-1;
dogcthr=new DogCatcher(this);
cout << "\n\n";
} // end AnimalControl constructor
// destructor
AnimalControl::~AnimalControl()
{
for(int i=startpos;i<=insertpos;i++)
{
cout << "AnimalControl destructor: "<< endl;
ptr_animal->~Animal;
}
cout << "\n\n";
} // end AnimalControl destructor
//incarcerate method
void AnimalControl::incarcerate(Animal *temp_ptr)
{
if(startpos==0&&insertpos==9)
{
cout<<"\nNo free slot..Memory full";
}
else //finding first available free slot
{
if(startpos==-1)
startpos=insertpos=0;
else if(insertpos==9)
insertpos=0;
else
insertpos++;
ptr_animal[insertpos]=temp_ptr;
}
}// end incarcerate method
void AnimalControl::disturbance()
{
for(int i=startpos;i<=insertpos;i++)
{
(ptr_animal)->vocalize();
}
}
I need to Templatize the AnimalControl class, then rename the AnimalControl class as ControlFacility. Convert it to be template class. And at the end all uses of class Animal are replaced with the template parameter T.
I know to use template with functions and how to declare. But not with class, and also how to define.
Can anyone please help me how to rename and templatize.
Any help will be very helpful.
Thanks,
Tina.
I am new in implementing Templates.
My .h file.
class AnimalControl
{
public:
DogCatcher* getDogCatcherRef();
void disturbance();
void incarcerate(Animal* temp_ptr);
AnimalControl(); // constructor
~AnimalControl(); // destructor
private:
int startpos; //start point of incarcerate
int insertpos; //first free slot
DogCatcher* dogcthr;
Animal* ptr_animal[10];
};
Inside my .cpp file
AnimalControl::AnimalControl()
{
cout << "AnimalControl constructor: " << endl;
startpos=insertpos=-1;
dogcthr=new DogCatcher(this);
cout << "\n\n";
} // end AnimalControl constructor
// destructor
AnimalControl::~AnimalControl()
{
for(int i=startpos;i<=insertpos;i++)
{
cout << "AnimalControl destructor: "<< endl;
ptr_animal->~Animal;
}
cout << "\n\n";
} // end AnimalControl destructor
//incarcerate method
void AnimalControl::incarcerate(Animal *temp_ptr)
{
if(startpos==0&&insertpos==9)
{
cout<<"\nNo free slot..Memory full";
}
else //finding first available free slot
{
if(startpos==-1)
startpos=insertpos=0;
else if(insertpos==9)
insertpos=0;
else
insertpos++;
ptr_animal[insertpos]=temp_ptr;
}
}// end incarcerate method
void AnimalControl::disturbance()
{
for(int i=startpos;i<=insertpos;i++)
{
(ptr_animal)->vocalize();
}
}
I need to Templatize the AnimalControl class, then rename the AnimalControl class as ControlFacility. Convert it to be template class. And at the end all uses of class Animal are replaced with the template parameter T.
I know to use template with functions and how to declare. But not with class, and also how to define.
Can anyone please help me how to rename and templatize.
Any help will be very helpful.
Thanks,
Tina.