Reputation: 171
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.
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"; }
Upvotes: 0
Views: 169
Reputation: 409166
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);
}
Upvotes: 2