Question
C++ HW: This homework is intended to test your knowledge on template functions and overloaded operators. You will be asked to write a bubble sorting
C++ HW:
This homework is intended to test your knowledge on template functions and overloaded operators. You will be asked to write a bubble sorting program on template class.
Bubble sorting is a prevailing method to sort an array in either descending or ascending order. Before you start, make sure youre able to write a small bubble-sorting function to sort an int array, which has two functions as below
void swap(a, b)
void bubbleSorting(int array[ ], const int& size)
Try to use char, double, float array instead of int array, and test your program to make sure all work fine in descending or ascending order. Then a natural intuition comes out that you could use template function to re-write this bubble-sort function.
Write a template class Holder which has a private template member variable element which might be char, double, float, int or string type. You should design member functions as below
Default constructor Any appropriate operation inside is encouraged, and left empty is also fine
Constructor with one argument Pass in value of outside to element by argument
Destructor
Overloaded operator + =
Take a Holder object as argument, and would return a newly created Holder object with summed/equal element.
Overloaded stream << and >>
Appropriate accessor function
Besides, you should also design a non-member overloaded operator - which takes two Holder object and return a third object with difference value of element.
Write a main function to test bubble sorting for a Holder array.
1. Read from command line two positive integers. First int would be the size of Holder array (size is integers from 4 to 8), and second int would be either 1 or 2 (1 means ascending order and 2 means descending order).
Should judge ar g c 3 or not, should judge whether input numbers are positive and integers within designated range. For example, would test ./solution abc -2, ./solution -1.3 1.456, ./solution 10.346 string, and all these should fail and exit erroneously.
2. Create a Holder dynamic array
After creating the array, you should use overloaded stream to enable user to define some (not all) Holder objects in the array, and rest of objects in the array should be defined by calling all different overloaded + - = operators.
3. Call overloaded stream >> to print the current array (before sorted)
4. Bubble sort the Holder array by invoking your bubbleSort function, and call overloaded stream to print the current array (after sorted). Make sure the array is actually sorted well! The class right now might still lack some important overloaded operator for bubble sorting. Which operator is still missing? You should be able to figure it out by thinking, discussing and google.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started