类规范ADTbag.h,驱动程序 我在学习C++方面比较新。我很困惑,任何帮助都将不胜感激。我在课堂上得到了(main.cpp)和(ADTbag.h),并被告知要完成课堂实施(“ADTbag.cpp”),因为老师有语言问题,从教授那里学到任何东西都非常困难。我的问题是这两个(main.cpp)和(ADTbag.h)是否要合并?如此困惑:我只是需要一个关于如何处理这种痛苦的虚假解释。以下是课堂上给出的代码: #include "ADTbag.h" #include<string> #include<iostream> using namespace std; void displayBag(ArrayBag<string>& bag) { cout << "The bag contains "<< bag.getCurrentSize() << " items:\n"; vector<string> bagItems=bag.toVector(); int numberOfEntries=(int)bagItems.size(); for (int i=0; i<numberOfEntries; i++) { cout <<bagItems[i] << " "; } cout << endl; } void main() { ArrayBag<string> bag; cout << "Testing the array-based bag:" <<endl; if (bag.isEmpty()) cout << "The initial bag is empty.\n"; displayBag(bag); string items[]={"one", "two", "three", "four", "five", "one", "two"}; cout << "Add 7 items to the bag:" <<endl; for (int i=0; i<7; i++) { bag.add(items[i]); } displayBag(bag); cout << "The string \"one\" occurs " << bag.getFrequencyOf("one") << " times.\n"; cout << "The string \"two\" occurs " << bag.getFrequencyOf("two") << " times.\n"; cout << "The string \"three\" occurs " << bag.getFrequencyOf("three") << " time.\n"; cout << "The string \"seven\" occurs " << bag.getFrequencyOf("seven") << " time.\n"; cout<< "Remove string \"one\" ...\n"; bag.remove("one"); displayBag(bag); cout<< "Remove string \"two\" ...\n"; bag.remove("two"); displayBag(bag); if (bag.contains("three")) cout << "The bag contains the string \"three\".\n"; else cout << "The bag does NOT contain the string three.\n"; if (bag.contains("seven")) cout << "The bag contains the string \"seven\".\n"; else cout << "The bag does NOT contain the string \"seven\".\n"; cout << "Delete all items fron the bag. \n"; bag.clear(); displayBag(bag); #包括“ADTbag.h” #包括 #包括 使用名称空间std; 无效显示袋(ArrayBag和bag) { 您可能忘记添加ADTbag.h的源代码(据我所知,您的两个源代码是相同的)。但基本理论非常简单: 将所有三个文件(main.cpp、ADTbag.h和ADTbag.cpp)放在一个目录中 添加行#在ADTbag.cpp中包含“ADTbag.h”,并在ADTbag.cpp中添加(ADTbag.h)中所有函数的实现

类规范ADTbag.h,驱动程序 我在学习C++方面比较新。我很困惑,任何帮助都将不胜感激。我在课堂上得到了(main.cpp)和(ADTbag.h),并被告知要完成课堂实施(“ADTbag.cpp”),因为老师有语言问题,从教授那里学到任何东西都非常困难。我的问题是这两个(main.cpp)和(ADTbag.h)是否要合并?如此困惑:我只是需要一个关于如何处理这种痛苦的虚假解释。以下是课堂上给出的代码: #include "ADTbag.h" #include<string> #include<iostream> using namespace std; void displayBag(ArrayBag<string>& bag) { cout << "The bag contains "<< bag.getCurrentSize() << " items:\n"; vector<string> bagItems=bag.toVector(); int numberOfEntries=(int)bagItems.size(); for (int i=0; i<numberOfEntries; i++) { cout <<bagItems[i] << " "; } cout << endl; } void main() { ArrayBag<string> bag; cout << "Testing the array-based bag:" <<endl; if (bag.isEmpty()) cout << "The initial bag is empty.\n"; displayBag(bag); string items[]={"one", "two", "three", "four", "five", "one", "two"}; cout << "Add 7 items to the bag:" <<endl; for (int i=0; i<7; i++) { bag.add(items[i]); } displayBag(bag); cout << "The string \"one\" occurs " << bag.getFrequencyOf("one") << " times.\n"; cout << "The string \"two\" occurs " << bag.getFrequencyOf("two") << " times.\n"; cout << "The string \"three\" occurs " << bag.getFrequencyOf("three") << " time.\n"; cout << "The string \"seven\" occurs " << bag.getFrequencyOf("seven") << " time.\n"; cout<< "Remove string \"one\" ...\n"; bag.remove("one"); displayBag(bag); cout<< "Remove string \"two\" ...\n"; bag.remove("two"); displayBag(bag); if (bag.contains("three")) cout << "The bag contains the string \"three\".\n"; else cout << "The bag does NOT contain the string three.\n"; if (bag.contains("seven")) cout << "The bag contains the string \"seven\".\n"; else cout << "The bag does NOT contain the string \"seven\".\n"; cout << "Delete all items fron the bag. \n"; bag.clear(); displayBag(bag); #包括“ADTbag.h” #包括 #包括 使用名称空间std; 无效显示袋(ArrayBag和bag) { 您可能忘记添加ADTbag.h的源代码(据我所知,您的两个源代码是相同的)。但基本理论非常简单: 将所有三个文件(main.cpp、ADTbag.h和ADTbag.cpp)放在一个目录中 添加行#在ADTbag.cpp中包含“ADTbag.h”,并在ADTbag.cpp中添加(ADTbag.h)中所有函数的实现,c++,arrays,C++,Arrays,因此,您ADTbag.cpp将如下所示: #include "ADTbag.h" ArrayBag::ArrayBag() { size = 0; // initialize empty list } bool ArrayBag::isEmpty() const { return (size == 0); // return true if list is empty } int ArrayBag::getCurrentSize() const { return size; /

因此,您ADTbag.cpp将如下所示:

#include "ADTbag.h"

ArrayBag::ArrayBag()
{
  size = 0; // initialize empty list
}

bool ArrayBag::isEmpty() const
{
  return (size == 0); // return true if list is empty
}

int ArrayBag::getCurrentSize() const
{
  return size; // return size of the list
}

// ... 

两个代码段都是相同的。感谢您的输入,我编辑了问题并添加了2个源代码。至于从哪里开始使用我丢失的ADTbag.cpp,我只是复制并粘贴两个给定的文件,然后从那里开始工作,还是真正要求我做什么?您必须创建ADTbag.cpp并实现所有函数,这些函数描述了在ADTbag.h中编辑。我将编辑我的答案以向您展示示例(见上文)。
#include "ADTbag.h"

ArrayBag::ArrayBag()
{
  size = 0; // initialize empty list
}

bool ArrayBag::isEmpty() const
{
  return (size == 0); // return true if list is empty
}

int ArrayBag::getCurrentSize() const
{
  return size; // return size of the list
}

// ...