程序未在某些IDE上运行,类出现问题

时间:2019-03-19 00:25:11

标签: c++ oop

有人可以帮助我在这里的代码中找到问题吗? 我不断收到错误消息,提示程序已终止。它运行在某些思想上,但不是在其他思想上运行,而且很痛苦,因为我觉得我已经开始掌握OOP的概念了,但猜想不是。该程序的主要思想是将形状数据读入指针数组,然后显示有关形状的所有信息。

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

// Global constant for array size.
const int SIZE = 99;

// Base Class
class Shape
{
protected:
  char type;
  int serNo;
  int dim1;
  int dim2;

public:
  // Default constructor
  Shape()
  {
    type = ' ';
    serNo = 0;
    dim1 = 0;
    dim2 = 0;
  }

  // Constructor #1
  Shape(char t, int s, int d1, int d2)
  {
    type = t;
    serNo = s;
    dim1 = d1;
    dim2 = d2;
  }

  // Get type
  char getType()
  {
    return type;
  }

  // Get serial number
  int getSerNo()
  {
    return serNo;
  }

  // Get dimension1
  int getDim1()
  {
    return dim1;
  }    

  // Get dimension2
  int getDim2()
  {
    return dim2;
  }

  // Destructor
  ~Shape()
  {}
};

// Circle Class (Derived from Shape)
class Circle : public Shape
{
public:
  Circle(char t, int s, int d1, int d2) : Shape(t, s, d1, d2)
  {}

};

// Spray Class (Derived from Circle)
class Spray : public Circle
{
public:
// Constructor
  Spray(char t, int s, int d1, int d2) : Circle(t, s, d1, d2)
  {}

};

// Rectangle Class (Derived from Shape)
class Rectangle : public Shape
{
public:
// Constructor
  Rectangle(char t, int s, int d1, int d2) : Shape(t, s, d1, d2)
  {}

};

// Square Class (Derived from Rectangle)
class Square : public Rectangle
{
public:
// Constructor
  Square(char t, int s, int d1, int d2) : Rectangle(t, s, d1, d2)
  {}

};

class mgrShape : public Shape
{
protected:
  int recordCount;
  Shape * shapeArr[SIZE] = {nullptr}; // Manager owns the main data 
//structure
public:
  // Constructor.
  mgrShape()
  {
    populateShapeData();
  }

  // Populate the shapeArr from file.
  void populateShapeData()
  {
    fstream inputFile;
    string fileName = "shaperecords.txt";
    inputFile.open(fileName);
    while(inputFile >> type >> serNo >> dim1 >> dim2)
    {
      if (type == 'C')
        shapeArr[recordCount] = new Circle(type, serNo, dim1, dim2);
      else if (type == 'R')
        shapeArr[recordCount] = new Rectangle(type, serNo, dim1, dim2);
      else if (type == 'S')
        shapeArr[recordCount] = new Spray(type, serNo, dim1, dim2);
      else if (type == 'Q')
        shapeArr[recordCount] = new Square(type, serNo, dim1, dim2);
      recordCount++;
    }
    inputFile.close();
  }

  //Display all of the shapes.
  void displayAll()
  {
    for (int i = 0; i < recordCount; i++)
    {
      if (shapeArr[i]->getType() == 'C')
      {
        cout << "Circle Found!" << endl;
        cout << shapeArr[i]->getType() << endl;
        cout << shapeArr[i]->getSerNo() << endl;
        cout << shapeArr[i]->getDim1() << endl;
        cout << shapeArr[i]->getDim2() << endl;
      }
      else if (shapeArr[i]->getType() == 'R')
      {
        cout << "Rectangle Found!" << endl;
        cout << shapeArr[i]->getType() << endl;
        cout << shapeArr[i]->getSerNo() << endl;
        cout << shapeArr[i]->getDim1() << endl;
        cout << shapeArr[i]->getDim2() << endl;
      }
      else if (shapeArr[i]->getType() == 'S')
      {
        cout << "\nSpray Found!" << endl;
        cout << shapeArr[i]->getType() << endl;
        cout << shapeArr[i]->getSerNo() << endl;
        cout << shapeArr[i]->getDim1() << endl;
        cout << shapeArr[i]->getDim2() << endl;
      }
      else if (shapeArr[i]->getType() == 'Q')
      {
        cout << "Square Found!" << endl;
        cout << shapeArr[i]->getType() << endl;
        cout << shapeArr[i]->getSerNo() << endl;
        cout << shapeArr[i]->getDim1() << endl;
        cout << shapeArr[i]->getDim2() << endl;
      }
    }
  }

// Clear the dynamically allocated memory from shapeArr.
~mgrShape()
{
  for (int i = 0; i < recordCount; i++)
    delete shapeArr[i];
}

};


int main()
{
  mgrShape ms;

  ms.displayAll();

  return 0;
}





//And here's the input file's contents

//C 1001 30 -1
//R 1002 14 10
//S 1003 30 20
//Q 1004 28 -1
//S 1005 30 75

0 个答案:

没有答案