Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question: Create a template for class GenericArray to print out the values i

ID: 3786408 • Letter: C

Question

C++ Question:

Create a template for class GenericArray to print out the values in arrays with data types: integer, float, or string.

Hints: Example of a class for integer data type is shown below. Note that the below is not a template class and your task is to convert it to a template class that will work for integer, float or string.

// this is only a partial example class array for integer data type.

// You need to rewrite it as a template class to support int, float,

// and string data types.

class GenericArray {

public:

      GenericArray(int array[], int arraysize); // constructor

      ~GenericArray();     // destructor

      void print();        // the print function

private:

      int *ptr;           

      int size;

};

// This main function requires the template class GenericArray to work and it can be used to test the template class that you created. Do not change.

int main() {

      // using integer data type

int arraya[5] = { 1, 2, 3, 4, 5 };

      GenericArray a(arraya, 5);

      a.print();

      // using float data type

      float arrayb[5] = { 1.012, 2.324, 3.141, 4.221, 5.327 };

      GenericArray b(arrayb, 5);

      b.print();

      // using string data type

      string arrayc[] = { "Ch1", "Ch2", "Ch3", "Ch4", "Ch5" };

      GenericArray c(arrayc, 5);

      c.print();

      return 0;

}

Write the complete code for the class template showing the header files, namespaces and:

i)      Template declaration

ii)     The Constructor

iii)      The Destructor

iv)     The print() function

v)      Test your class template by running the main() function above and capture the console screen output. Show the runtime screenshot.

Explanation / Answer

//Header Files

#include <iostream>

#include<iostream>

#include<string>

using namespace std;

//Template Declaration

template<class T>

class GenericArray{

               private:

               T a[5];

               T n;

               int i,j;

               public:

                      GenericArray(T ar[],T size){ // Constructor

                                             n=size;

                                             for(j=0;j<n;j++){

                                                            a[j] = ar[j];

                                             }

                              }

                      ~GenericArray(){   // Destructor

                              Cout<<”I am Destructor”;

                      }

                      void print(){ // Prints the data of Array

                              for(i=0;i<n;i++){

                                             cout<<a[i]<<" "<<endl;

                                 }

                      }

};

int main()

{

               int a[5]={1,2,3,4,5}; // Integer Template

               float fd[5]={1.1,2.2,3.3,4.4,5.5}; // Float Template

               double dd[5]={1.2,2.3,4.5,6.7,6.9}; // Double Template

               GenericArray<int> ga1(a,5); // Object of Integer

               GenericArray<float> ga3(fd,5); // Object of Float

               GenericArray<double> ga4(dd,5); // Object of Double

               cout<<"**************** ";

               ga1.print();

               cout<<"**************** ";

               ga3.print();

               cout<<"**************** ";

               ga4.print();

               cout<<"**************** ";

               return 0;

}

Output:

****************

1            

2            

3            

4            

5            

****************

1.1        

2.2        

3.3        

4.4        

5.5        

****************

1.2        

2.3        

4.5        

6.7        

6.9        

****************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote