2025年3月21日 星期五 甲辰(龙)年 月廿 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

C++模板类继承

时间:03-07来源:作者:点击数:62

继承可以应用于类模板。例如,在以下模板中,SearchableVector 类就是从 SimpleVector 类派生的:

  • //SearchVect.h 的内容
  • #include "IntRange.h"
  • template <class T>
  • class SearchableVector : public SimpleVector<T>
  • {
  • public:
  • // Constructor.
  • SearchableVector(int s) : SimpleVector<T>(s)
  • { }
  • // Copy constructor.
  • SearchableVector(const SearchableVector &);
  • // Additional constructor.
  • SearchableVector(const SimpleVector<T>&obj): SimpleVector<T>(obj) { }
  • int findItem(T);
  • };
  • template <class T>
  • SearchableVector<T>::SearchableVector(const SearchableVector &obj): SimpleVector<T>(obj)
  • {
  • }
  • template <class T>
  • int SearchableVector<T>::findItem(T item)
  • {
  • for (int count = 0; count < this->size (); count++)
  • {
  • if (this->operator[] (count) == item)
  • return count;
  • }
  • return -1;
  • }
提示,这里不再给出 SimpleVector.h 的代码,该代码可从《C++类模板用法》一节中获取。

现在使用该示例来仔细看一看从模板基类派生一个类的方法。首先,必须向编译器指出,我们正在基于另一个己经存在的类模板定义一个新的类模板,语句如下:

  • template<class T>
  • class SearchableVector : public SimpleVector<T>
  • {
  • //类的成员写在此处
  • };

这里新定义的类模板是 SearchableVector,而现有的基类模板是 SimpleVector <T>。该类有 3 个构造函数。以下显示的是第一个构造函数:

SearchableVector(int size) : SimpleVector<T>(size){ }

该构造函数被设计为动态分配一个T类型的 size 个元素的数组,它通过调用基类构造函数并给它传递形参 size 来完成。此构造函数将创建一个指定大小的数组,并将所有元素初始化为 T 类型的默认值。该类还有另一个构造函数如下:

SearchableVector(SimpleVector<T>&obj): SimpleVector<T>(obj){ }

它釆用一个基类对象作为形参,其副本将被搜索。构造函数只是将其形参传递给基类复制构造函数。剩余的一个构造函数是 SearchableVector 类的复制构造函数,如下所示:

SearchableVector(SearchableVector<T>&obj): SimpleVector<T>(obj){ }

由于 SearchableVector 的初始化与 SimpleVector 的初始化相同,所以 SearchableVector 复制构造函数只是将其参数传递给它的基类的复制构造函数。成员函数 findItem 釆用一个类型 T 的项目作为参数,并返回该项目在数组中的位置。如果在数组中找不到该项目,则返回值 -1。

下面程序演示了该类的用法。它将值存储到两个 SearchableVector 对象中,然后在每个对象中搜索一个特定值。

  • //This program demonstrates the SearchableVector template.
  • #include <iostream>
  • #include "searchvect.h"
  • using namespace std;
  • int main()
  • {
  • const int SIZE = 10;
  • SearchableVector<int> intTable (SIZE);
  • SearchableVector<double> doubleTable(SIZE);
  • // Store values in the vectors
  • for (int x = 0; x < SIZE; x++)
  • {
  • intTable[x] = (x * 2);
  • doubleTable[x] = (x * 2.14);
  • }
  • // Display the values in the vectors
  • cout << "These values are in intTable:\n";
  • for (int x = 0; x < SIZE; x++)
  • cout << intTable[x] << " ";
  • cout << endl;
  • cout << "These values are in doubleTable:\n";
  • for (int x = 0; x < SIZE; x++)
  • cout << doubleTable[x] << " ";
  • cout << endl;
  • // Now search for values in the vectors
  • int result;
  • cout << "Searching for 6 in intTable.\n";
  • result = intTable.findItem(6);
  • if (result == -1)
  • cout << "6 was not found in intTable. \n";
  • else
  • cout << "6 was found at subscript " << result << endl;
  • cout << "Searching for 12.84 in doubleTable.\n";
  • result = doubleTable.findItem(12.84);
  • if (result == -1)
  • cout << "12.84 was not found in doubleTable.\n";
  • else
  • cout << "12.84 was found at subscript " << result << endl;
  • return 0;
  • }

程序输出结果:

These values are in intTable:
0 2 4 6 8 10 12 14 16 18
These values are in doubleTable
0 2.14 4.28 6.42 8.56 10.7 12.84 14.98 17.12 19.26
Searching :for 6 in intTable.
6 was found at subscript.3
Searching for 12.84 in doubleTable.
12.84 was found at subscript 6

SearchableVector 类说明了可以从一个类模板派生出另一个类模板。另外,类模板可以从普通类派生,而普通类也可以从类模板派生。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门