C++ Help? Please? Class Templates In this experiment you will investigate a temp
ID: 3560193 • Letter: C
Question
C++ Help? Please? Class Templates
In this experiment you will investigate a template array class
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
/*********************************************************************/
// Class declaraton for Array_Class
/*********************************************************************/
template <class New_Type>
class Array_Class
{
public:
Array_Class();
~Array_Class();
void Add(New_Type item);
int Search(New_Type item);
void Print();
private:
New_Type *A;
int count;
};
/*********************************************************************/
// Class definitions for the member function of Array_Class
/*********************************************************************/
template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
cout << "You are inside the default constructor. ";
cout << "New_Type has a size of " << sizeof(New_Type) << " bytes ";
count=0;
A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
cout << "The Destructor has been called. ";
delete [] A;
count = 0;
A = 0;
}
template <class New_Type>
void Array_Class<New_Type>::Add(New_Type item)
{
if (count<SIZE)
{
A[count++] = item;
}
else
{
cout << "The array is full. ";
}
}
template <class New_Type>
int Array_Class<New_Type>::Search(New_Type item)
{
int i;
for(i=0; i<count; i++)
{
if (item == A[i])
{
return i;
}
}
return -1;
}
template <class New_Type>
void Array_Class<New_Type>::Print()
{
int i;
for(i=0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
int main()
{
return 0;
}
(Notice that every member function is prefaced with the template heading, even functions that do not use the type parameter. )
Add code to the main() function for the following:
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
/*********************************************************************/
// Class declaraton for Array_Class
/*********************************************************************/
template <class New_Type>
class Array_Class
{
public:
Array_Class();
~Array_Class();
void Add(New_Type item);
int Search(New_Type item);
void Print();
bool Is_Empty();
bool Is_Full();
void Remove(New_Type item);
private:
New_Type *A;
int count;
};
/*********************************************************************/
// Class definitions for the member function of Array_Class
/*********************************************************************/
template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
cout << "You are inside the default constructor. ";
cout << "New_Type has a size of " << sizeof(New_Type) << " bytes ";
count=0;
A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
cout << "The Destructor has been called. ";
delete [] A;
count = 0;
A = 0;
}
template <class New_Type>
void Array_Class<New_Type>::Add(New_Type item)
{
if (count<SIZE)
{
A[count++] = item;
}
else
{
cout << "The array is full. ";
}
}
template <class New_Type>
int Array_Class<New_Type>::Search(New_Type item)
{
int i;
for(i=0; i<count; i++)
{
if (item == A[i])
{
return i;
}
}
return -1;
}
template <class New_Type>
void Array_Class<New_Type>::Print()
{
int i;
for(i=0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
template <class New_Type>
bool Array_Class<New_Type>::Is_Empty()
{
return (count==0);
}
template <class New_Type>
bool Array_Class<New_Type>::Is_Full()
{
return (count == SIZE);
}
template <class New_Type>
void Array_Class<New_Type>::Remove(New_Type item)
{
int index = Search(item);
if(index>0)
{
for(int i=index; i<count-1; i++)
A[i] = A[i+1];
count--;
}
else
cout << item <<" Not found in Array. not possible to remove from array " << endl;
}
//(Notice that every member function is prefaced with the template heading, even functions that do not use the type parameter. )
//Add code to the main() function for the following:
int main()
{
//Declaring an Array_Class object of strings called
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.