c++ - 抽象类输出问题

标签 c++ abstract-class virtual-functions dynamic-arrays

这个主程序应该要求用户输入一些数字并将它们存储到一个动态数组中。然后应该以直线输出数组的内容,没有结束行命令,中间有一个逗号。我不知道如何启动该程序。

如果你们能帮我找到一个方法来做到这一点,我将永远感激不尽!

这是 ListType.h:

#ifndef LISTTYPE_H_INCLUDED
#define LISTTYPE_H_INCLUDED
#include <iostream>

class ListType {
public:
 ListType(size_t=10);
 virtual ~ListType();
 virtual bool insert(int)=0;
 virtual bool erase();
 virtual bool erase(int)=0;
 virtual bool find(int) const=0;
 size_t size() const;
 bool empty() const;
 bool full() const;
 void output(std::ostream& out) const;
 friend std::ostream& operator << (std::ostream&, const ListType&);
protected:
 int *items;
 size_t capacity;
 size_t count;
};

#endif // LISTTYPE_H_INCLUDED

这里是 UListType.h:

#ifndef ULISTTYPE_H_INCLUDED
#define ULISTTYPE_H_INCLUDED
#include <iostream>

class UListType: public ListType {
public:
 UListType(size_t=10);
 bool insert(int);
 bool erase(int);
 bool find(int) const;
};


#endif // ULISTTYPE_H_INCLUDED

这里是 OListType.h:

#ifndef OLISTTYPE_H_INCLUDED
#define OLISTTYPE_H_INCLUDED
#include <iostream>

class OListType: public ListType {
public:
 OListType(size_t=10);
 bool insert(int);
 bool erase(int);
 bool find(int) const;
};


#endif // OLISTTYPE_H_INCLUDED

这里是 ListType.cpp:

#include "ListType.h"

ListType::ListType (size_t a) {
 capacity = a;
 count = 0;
 items = new int [capacity];
}

ListType::~ListType() {
 delete [] items;
}

bool ListType::erase() {
 count = 0;
 return 0;
}

size_t ListType::size() const {
 return (count);
}

bool ListType::empty() const {
 return (count == 0);
}

bool ListType::full() const {
 return (count == capacity);
}

void ListType::output(std::ostream& out) const {
 for (int i = 0; i < count; i++) {
      if (i > 0) {
        out << ", ";
      }
      out << items[i];
 }
}

std::ostream& operator << (std::ostream& out, const ListType& my_list) {
 my_list.output(out);
   return out;
}

这里是 UListType.cpp

#include "ListType.h"
#include "UListType.h"

UListType::UListType (size_t c): ListType(c) {}

bool UListType::insert(int item) {
 if (full()) {
    int *newitems;
    capacity *=2;
    newitems = new int[capacity];
    for (size_t i =0; i < count; ++i){
        newitems[i] = items[i];
    }
    delete [] items;
    items = newitems;
 }
 items[count++] = item;
 return true;
}

bool UListType::erase(int item) {
 bool result = false;
 size_t i=0;
 while ( i < count && items [i] != item) {
     ++i;
}
 if (i < count) {
    items[i] = items[-- count];
    result = true;
 }
 return result;
}

bool UListType::find(int item) const {
 size_t i = 0;
 while (i < count && items [i] != item) {
     ++i;
 }
 return i;
}

这里是 OListType.cpp

#include "ListType.h"
#include "OListType.h"

OListType::OListType(size_t c): ListType(c) {}

bool OListType::insert(int item) {
 size_t i = count;
 if (full()) {
    int *newitems;
    capacity *=2;
    newitems = new int[capacity];
    while (i > 0 && items[i-1] > item){
        newitems[i] = items[i];
    }
    delete [] items;
    items = newitems;
 }
 items[count++] = item;
 return true;
}

bool OListType::erase(int item) {
 bool found=false;
 size_t i=0, j= count-1, mid;
 while (i <= j && !(found)){
    mid = (i + j)/2;
    if (item < items [mid])
        j = mid - 1;
    else if (item > items [mid])
        i = mid + 1;
    found = items [mid] == item;
}
if (found) {
    for (i = mid; i < count - 1; ++i) {
        items [i] = items [i +1];
    }
     --count;
 }
 return found;
}

bool OListType::find (int item) const {
bool found=false;
 size_t i=0, j= count-1, mid;
 while (i <= j && !(found)){
    mid = (i + j)/2;
    if (item < items [mid])
        j = mid - 1;
    else if (item > items [mid])
        i = mid + 1;
    found = items [mid] == item;
 }
 return found;
}

最佳答案

#include "ListType.h"
#include "UListType.h"

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
  UListType UL;

  cout << "How many numbers do you want to put it?" << endl;
  int n;
  cin >> n;
  cout << "All right, enter " << n << " numbers:" << endl;
  int x;
  for(int k=0; k<n; ++k)
  {
    cin >> x;
    // do something with x
  }
  return(0);
}

关于c++ - 抽象类输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509715/

相关文章:

c# - 使用其类型的向下转型实例

scala - 如何将 "instantiate"抽象类放在父类(super class)中?

c++ - 是否只覆盖虚方法

c++ - 程序终止后动态分配的内存

java - Java 中的通用返回类型

c++ - 有没有一种方法可以在不修改基类的情况下防止派生类中的内存泄漏?

c++ - 如果调用非虚拟实现,是否会通过 vtable 进行函数分派(dispatch)?

C++:为什么使用 ".hh"作为 C++ 头文件的扩展名的原因

c++ - SFML 不适用于 C++

C++/OpenCV - 如何 reshape cv::Mat?