Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Create Template Class object of auto type in C++?

I am a learner and the logic I’m using may not be correct (since I’m still in the learning phase).
I have created a simple heap sort code in C++ using a template. In the main function, calling every function from the Heap class for each datatype is creating copy of same lines. So, I thought to write the common function calls for all datatype using a pointer object and then that pointer object will refer to the corresponding objects of that Heap class.

Here is the part of the main function code in which I have error.

Heap<auto> *obj;
Heap<int> intOb(n);
Heap<char> charOb(n);
Heap<float> floatOb(n);

if (dataType == 1) {
    obj = &intOb;
} else if (dataType == 2) {
    obj = &charOb;

} else if (dataType == 3) {
    obj = &floatOb;

} else {
    cout << "Wrong Data Type\n";
    exit(101); }

obj->inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj->printArray();
obj->buildMaxHeap();
cout << "The Heapified Array is ";
obj->printArray();
obj->heapSort();
cout << "The Array After Heap Sort is ";
obj->printArray();
cout << "The Total no of comparisons is " << obj->counter << endl;

If I just call all these functions inside the if else loops, the codes will be repeated. In order to avoid them, I have created a pointer object of type auto. But, auto is not allowed in template. If I’m replacing auto with int, then the referencing for char and float is giving an error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to tweak the object creation so that I do not have to repeat these lines again and again in the if else loop?

If my issue will not be fixed, I’ll have to repeat same lines multiple times just like the given code.

if (dataType == 1) {
    Heap<int> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else if (dataType == 2) {
    Heap<char> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else if (dataType == 3) {
    Heap<float> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else { cout << "Wrong Data Type\n"; }
if (dataType == 1) {
    Heap<int> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else if (dataType == 2) {
    Heap<char> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else if (dataType == 3) {
    Heap<float> obj(n);
    obj.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    obj.printArray();
    obj.buildMaxHeap();
    cout << "The Heapified Array is ";
    obj.printArray();
    obj.heapSort();
    cout << "The Array After Heap Sort is ";
    obj.printArray();
    cout << "The Total no of comparisons is " << obj.counter << endl;

} else { cout << "Wrong Data Type\n"; }

>Solution :

Use a (template) function to do all the main work, and pass (a reference to) the Heap template instance to the function.

Perhaps something like:

template<typename T>
void work_on_heap(Heap<T> const& heap)
{
    heap.inputArray();
    cout << "---------------------------------------------------------\n";
    cout << "The Array you have entered is ";
    heap.printArray();
    heap.buildMaxHeap();
    cout << "The Heapified Array is ";
    heap.printArray();
    heap.heapSort();
    cout << "The Array After Heap Sort is ";
    heap.printArray();
    cout << "The Total no of comparisons is " << heap.counter << endl;
}

Then call like:

if (dataType == 1) {
    work_on_heap(Heap<int>(n));
} else if (dataType == 2) {
    work_on_heap(Heap<char>(n));
} else if (dataType == 3) {
    work_on_heap(Heap<float>(n));
} else {
    cout << "Wrong Data Type\n";
    exit(1);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading