/* Filename: templat2.cpp Author: Br. David Carlson Date: June 28, 1999 Last Revised: December 23, 2001 This program has the user enter data into an array of objects, prints the array, sorts it, and then prints it again. Tested with: Microsoft Visual C++ 2019 */ #include using namespace std; class ItemType { private: int ID; float Cost; public: ItemType(int IDNum = 0, float CostNum = 0.0); friend ostream & operator<<(ostream & OutputStream, const ItemType & Item); bool operator<(const ItemType & RHS) const; bool operator==(const ItemType & RHS) const; bool operator>(const ItemType & RHS) const; ItemType & operator=(const ItemType & RHS); }; /* Given: IDNum An integer ID number. CostNum The cost for the item with ID IDNum. Task: This is the default constructor. It creates an object of type ItemType that contains IDNum and CostNum. Return: Nothing directly, but it creates the implicit object. */ ItemType::ItemType(int IDNum, float CostNum): ID(IDNum), Cost(CostNum) { } /* Given: RHS An existing ItemType object. Task: This is an overloaded assignment operator. It copies all of the data from the RHS object into the implicit object. Return: A reference to the implicit object is returned in the function name so as to allow one to chain assignments together as in: ItemA = ItemB = ItemC; The implicit object is modified. */ ItemType & ItemType::operator=(const ItemType & RHS) { ID = RHS.ID; Cost = RHS.Cost; return *this; } /* Given: RHS An ItemType object (the right-hand side of the < ). Task: To compare the ID number of the implicit ItemType object to that of the RHS. Return: In the function name return true if the implicit object is less than RHS in ID number, false otherwise. */ bool ItemType::operator<(const ItemType & RHS) const { if (ID < RHS.ID) return true; else return false; } /* Given: RHS An ItemType object (the right-hand side of the < ). Task: To compare the ID number of the implicit ItemType object to that of the RHS. Return: In the function name return true if the implicit object is equal to RHS in ID number, false otherwise. */ bool ItemType::operator==(const ItemType & RHS) const { if (ID == RHS.ID) return true; else return false; } /* Given: RHS An ItemType object (the right-hand side of the < ). Task: To compare the ID number of the implicit ItemType object to that of the RHS. Return: In the function name return true if the implicit object is greater than RHS in ID number, false otherwise. */ bool ItemType::operator>(const ItemType & RHS) const { if (ID > RHS.ID) return true; else return false; } /* Given: OutputStream An output file stream. Item An ItemType object. Task: To print the contents of Item onto the OutputStream. Return: The modified file stream is returned in the function name. This allows one to chain together more than one << operator as in: cout << ItemA << endl << ItemB << endl; */ ostream & operator<<(ostream & OutputStream, const ItemType & Item) { OutputStream << "ID: " << Item.ID << " " << "Cost: " << Item.Cost; return OutputStream; } const int ArrayMax = 10; typedef ItemType DataArrayType[ArrayMax]; /* Given: DataArray Array, with data from index 0 to index Count - 1. Count The number of data items in DataArray. Task: To print the data in DataArray. Assumes: That << works for data of type T. Return: Nothing. */ template void PrintArray(T DataArray[], int Count) { int k; for (k = 0; k < Count; k++) cout << DataArray[k] << endl; cout << endl; } /* Given: First An item of type T. Second An item of type T. Task: To swap the values in First and Second. Return: First Second */ template void Swap(T & First, T & Second) { T Temp; Temp = First; First = Second; Second = Temp; } /* Given: DataArray An array to be sorted, with data from index 0 to index Count - 1. Count The number of items in DataArray. Task: To bubblesort DataArray into ascending order. Return: DataArray The sorted array. */ template void BubbleSort(T DataArray[], int Count) { int Top, k; bool Done; Top = Count - 1; Done = false; while ((! Done) && (Top > 0)) { Done = true; for (k = 0; k < Top; k++) if (DataArray[k] > DataArray[k + 1]) { Done = false; Swap(DataArray[k], DataArray[k + 1]); } Top--; } } /* Given: Nothing. Task: To have the user enter data and place it in DataArray. Return: DataArray The array of data. Count The number of items placed into DataArray. */ void LoadArray(DataArrayType DataArray, int & Count) { int TempID; float TempCost; Count = 0; cout << "Enter a positive integer for the ID number (0 to quit): "; cin >> TempID; while ((TempID != 0) && (Count < ArrayMax)) { cout << "Enter the cost of this item: "; cin >> TempCost; ItemType Item(TempID, TempCost); DataArray[Count] = Item; Count++; cout << "Enter a positive integer for the ID number (0 to quit): "; cin >> TempID; } } int main(void) { DataArrayType DataArray; int Size; LoadArray(DataArray, Size); cout << endl << "The original unsorted array:" << endl; PrintArray(DataArray, Size); BubbleSort(DataArray, Size); cout << endl << "The sorted array:" << endl; PrintArray(DataArray, Size); return 0; }