C++模板类的继承1 :模板类继承模板类
来源:互联网 发布:java培训骗局 编辑:程序博客网 时间:2024/04/25 13:46
大学时代旧作。
模板类的继承是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 ADTpublic: 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];};// constructortemplate<class List_entry>List<List_entry>:: List(){ count = 0;}/***** 其余代码略***********/#endif
2. 从模板类中派生一个模板子类
#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; }
- 模板类的继承
- 类模板的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- C++模板类的继承1 :模板类继承模板类
- 继承QVector----模板类的继承
- 概率算法求解圆周率π
- 几何方法---测试盒子 二
- TreeView
- ligh oj 1004 - Monkey Banana Problem
- hdu 2254 奥运
- C++模板类的继承1 :模板类继承模板类
- API相关工作的个人总结_Sandcastle简要使用介绍
- Android学习系列-把文件保存到SD卡上面(6)
- HDU4514(非连通图的环判断与图中最长链)
- one of the key features of distributed application-managemeability
- opencv之绘图
- [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo
- wikioi p1169 传纸条
- PHP 文件上传后台处理脚本