大学时代旧作。
模板类的继承是C++中较为高级的一种用法,其语法与普通C++类的继承略有差别。本文实例演示了一个通过C++模板类继承实现排序算法的案例。代码如下:
1. 实现List基类
#ifndef LIST_H #define LIST_H #include <iostream> using std::cout; using std::endl; enum Error_code { underflow, overflow, range_error, success, not_present,fail}; const int max_list = 100; template <class List_entry> class List { // methods of List ADT public: List(); int size() const; bool full() const; bool empty() const; void clear(); void traverse(void(*vist)(List_entry&)); // Traverse every elements in the List Error_code retrieve(int position, List_entry &x) const; Error_code replace(int position, const List_entry& x); Error_code remove(int position, List_entry& x); Error_code insert(int position, const List_entry &x); protected: // The contiguous implementation is based on the array int count; List_entry entry[max_list]; }; // constructor template<class List_entry> List<List_entry>:: List() { count = 0; } /***** 其余代码略***********/ #endif
#ifndef SORTABLE_LIST #define SORTABLE_LIST #include "list.h" template<class Record> class Sortable_list : public List<Record> { public: Sortable_list(); // Insertion sorting void insertion_sort(); // Selection sorting void selection_sort(); int max_key(int low, int high); void swap(int low, int high); // Shell sorting void shell_sort(); void sort_interval(int low, int increment); // Quick sorting void quick_sort(); void recursive_quick_sort(int low, int high); int partition(int low, int high); // Heap sorting void heap_sort(); void insert_heap(const Record& current, int low, int high); void build_heap(); }; // 派生类的构造函数,注意其语法差别 template<class Record> Sortable_list<Record>::Sortable_list():List<Record>(){} /********************其余代码略**************************************/ #endif
3. 测试代码
#include <iostream> using std::cout; using std::endl; #include "list.h" #include "Sortable_list.h" void print(int &a) { cout<<a<<" "; } int main() { Sortable_list<int> myList; myList.insert(0,6); myList.insert(0,56); myList.insert(0,7); myList.insert(2,57); myList.insert(1,4); myList.insert(3,23); myList.insert(0,9); myList.insert(5,45); myList.insert(4,17); myList.insert(0,7); myList.insert(6,89); myList.insert(6,34); myList.insert(2,35); // Sortable list to test different soring methods Sortable_list<int> myList2 = myList; Sortable_list<int> myList3 = myList; Sortable_list<int> myList4 = myList; Sortable_list<int> myList5 = myList; // Print out the original list cout<<"All the entries in the original list: "<<endl; myList.traverse(print); cout<<endl; // Test the insert sorting cout<<"After the insert soring, we get:"<<endl; myList.insertion_sort(); myList.traverse(print); cout<<endl; // Test the selection sorting cout<<"After the insert soring, we get:"<<endl; myList2.selection_sort(); myList2.traverse(print); cout<<endl; // Test the shell sorting cout<<"After the shell sort,we get :"<<endl; myList3.shell_sort(); myList3.traverse(print); cout<<endl; // Test the quick sorting cout<<"After the quick sort,we get :"<<endl; myList4.quick_sort(); myList4.traverse(print); cout<<endl; // Test the heap sorting cout<<"After the quick sort,we get :"<<endl; myList5.heap_sort(); myList5.traverse(print); cout<<endl; return 0; }