-3

これは小さな C++ コンソール プロジェクトです。UNIX環境でコンパイルするためにg ++を使用しています。

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;
}

4

1 に答える 1

2

スライスの問題に加えて、そのような継承されたクラスで親コンストラクターを呼び出さず、コンストラクター初期化子リストに入れます。

Bus (string rego, string make, string model, int seats, int weight)
    : Vehicles(rego, make, model, seats, weight, "This is a bus record")
{ }
于 2013-10-14T09:33:44.267 に答える