【问题标题】:C++ class object array - adding new records [closed]C++ 类对象数组 - 添加新记录 [关闭]
【发布时间】:2013-10-14 09:27:36
【问题描述】:

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

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

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

基类:车辆

派生类 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;
}

【问题讨论】:

  • 您可能想阅读例如this old question and its answers.
  • 有些东西不工作......什么?您应该将问题缩小具体问题。大多数 SO 用户不想为您启动调试会话...
  • using namespace std; 在标题中通常是个坏主意。
  • 我已经提到了这个问题。当我使用 showVehicle() 时,它不会向控制台写入任何内容。
  • @Adriano 我做了类似于你建议的事情。我已将 cout 放入 Vehicles 构造函数中,以查看传递的值是否到达那里。我可以在那里看到传递的值。但是,当我想将它们从数组中写入控制台时,什么都不会打印。

标签: c++ class console


【解决方案1】:

除了切片问题,你不要调用继承类中的父构造函数,你把它放在构造函数初始化列表中:

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

【讨论】:

  • 如果我这样做我会得到错误:expected unqualified-id before '{' token In constructor 'Bus::Bus(std::string, std::string, std::string, std::string)':
  • 回答时如何换行 :( 对不起,我是新人
  • 您不希望; 位于Vehicles(...) 行的末尾。
  • @Ozgur 我的回答有误,现在更新
  • @RetiredNinja 哎呀,忘了删除它。现在完成,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多