C ++类对象数组 - 添加新记录

时间:2013-10-14 09:27:37

标签: c++ class console

这是一个小型的C ++控制台项目。我正在使用g ++在UNIX环境中编译它。

g ++ test.cpp -o test -ansi -Wall -pedantic(我必须使用这种语法)

我有一个基类和2个派生类。

基类:车辆

派生类1:汽车

派生类2:公共汽车

我有填充的测试数据。在数据填充结束时,我想将所有记录写入控制台,以查看数据填充是否正常。我得到的只是空屏幕。不知何故,某些东西不起作用。无法理解。

数据填充必须在另一个名为utility.cpp的cpp文件中运行。

以下是我的文件:

//
// Vehicles.h
//
// Parent Vehicles class
//

#ifndef __VEHICLES_H__
#define __VEHICLES_H__


using namespace std;

class Vehicles
{
   protected:
      string rego; 
      string make; 
      string model; 
      int seats; 
      int weight;
      string type;

   public:
      Vehicles () { }

      Vehicles (string xrego, string xmake, string xmodel, int xseats, int xweight, 
                string xtype) 
      {

         rego = xrego;
         make = xmake;
         model = xmodel;
         seats = xseats;
         weight = xweight;
         type = xtype; 

      }
      void showVehicle() {

         cout << "\n" << rego << "\t" << make << "\t" << model << "\t"              
              << seats << "\t" << weight << "\t" << xtype;
      }


};
#endif

// car.h
//
// derived class
//

#ifndef __CAR_H__
#define __CAR_H__

class Car : public Vehicles
{

   public:
      Car () {}
      Car (string rego, string make, string model, int seats, int weight) { 

         Vehicles(rego, make, model, seats, weight, "This is a car record");
      }

};

#endif

//
// bus.h
//
// derived class
//

#ifndef __BUS_H__
#define __BUS_H__


class Bus : public Vehicles
{

   public:
      Bus () {}
      Bus (string rego, string make, string model, int seats, int weight) { 

         Vehicles(rego, make, model, seats, weight, "This is a bus record");
      }

};

#endif

//
// utility.h
//

#ifndef __UTILITY_H__
#define __UTILITY_H__

#include <string>
#include <iostream>
#include <stdlib.h>

extern Vehicles Vehicle_List[];

using std::string;
using namespace std;

#endif

// utility.cpp

#include "utility.h"


void populate_test_data()
{
   const int ARRAY_SIZE_X = 8;
   const int ARRAY_SIZE_Y = 6;   
   int x;

   string data[ARRAY_SIZE_X][ARRAY_SIZE_Y] = { 
      {"AAA111","FORD","FALCON","5","1500","CAR"},
      {"BBB222","HOLDEN","CRUZE","5","1300","CAR"},
      {"CCC333","TOYOTA","YARIS","4","1050","CAR"},
      {"DDD444","FORD","ESCAPE","5","1500","CAR"},
      {"EEE555","HOLDEN","CAPTIVA","7","1400","CAR"},
      {"FFF666","TOYOTA","COROLLA","5","1400","CAR"},
      {"GGG777","MERCEDES","TRAVEGO","60","3200","BUS"},
      {"HHH888","SCANIA","NONAME","55","3500","BUS"}
   };

   for(x = 0; x < ARRAY_SIZE_X; x++) { 

      if(data[x][5] == "CAR") {
         Vehicle_List[x] = Car(data[x][0], data[x][1], data[x][2],
                                  data[x][3], data[x][4]);
      }
      else {
         Vehicle_List[x] = Bus(data[x][0], data[x][1], data[x][2],
                                  data[x][3], data[x][4]);
      }          
   }

   system("clear");

   for(x = 0; x < ARRAY_SIZE_X; x++) { 
      cout << "\n" << x << ". ";
      Vehicle_List[x].showVehicle();
   }

   sleep(60);   
}

//
// driver.h
//
// header file for the driver program
//

#ifndef __DRIVER_H__
#define __DRIVER_H__

#include <iostream>
#include <string>
#include <stdlib.h>

#include "vehicles.h"
#include "bus.h"
#include "car.h"

using namespace std;

void populate_test_data();

#endif

// driver.cpp

#include "driver.h"

const int BOOKING_SIZE = 100;
Vehicles Vehicle_List[BOOKING_SIZE];

int main()
{

   populate_test_data();

   return 0;
}

1 个答案:

答案 0 :(得分:2)

除了切片问题之外,你没有调用继承类中的父构造函数,你将它放在构造函数初始化列表中:

Bus (string rego, string make, string model, int seats, int weight)
    : Vehicles(rego, make, model, seats, weight, "This is a bus record")
{ }