-3

This is a small C++ console project. I am using g++ to compile it on UNIX environment.

g++ test.cpp -o test -ansi -Wall -pedantic (I have to use this syntax)

I have a base class and 2 derived classes.

Base class : Vehicles

Derived class 1: Cars

Derived class 2: Buses

I have test data to populate. At the end of the data population I want to write all records onto the console to see if data population is working okay or not. All I get is empty screen. Somehow something doesn't work. Couldn't figured it out.

Data population has to run in another cpp file which called utility.cpp.

Here are my files:

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

Oscar
  • 233
  • 2
  • 6
  • 15
  • You might want to read e.g. [this old question and its answers](http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c/274654#274654). – Some programmer dude Oct 14 '13 at 09:30
  • Something doesn't work...what? You should **narrow** your problem to a **specific question**. Most SO users wouldn't like to start a debug session for you... – Adriano Repetti Oct 14 '13 at 09:31
  • 1
    `using namespace std;` in a header is generally a bad idea. – Retired Ninja Oct 14 '13 at 09:35
  • I already mentioned the problem. When I use showVehicle() it writes nothing to the console. – Oscar Oct 14 '13 at 09:36
  • @Adriano I have done something similar to what you suggest. I have placed cout into the Vehicles constructor to see if passed values are arriving there or not. I can see passed values there. However when I want to write them to console from the array nothing prints. – Oscar Oct 14 '13 at 10:06
  • Seems like it's time to break out the debugger. I'd also try removing the `system("clear");` in case it is interfering. – Retired Ninja Oct 14 '13 at 10:11
  • I am not using any editor as files are on UNIX system. So I don't have luxury of using debugging options. Anyways, thanks a lot to all of you. It works now. You guys are life saver :) – Oscar Oct 14 '13 at 10:14
  • It's probably not causing your problems, but you shouldn't use [reserved names](http://stackoverflow.com/users/204847) for include guards (or for anything else). – Mike Seymour Oct 14 '13 at 11:07

1 Answers1

2

Besides the slicing problem, you don't call the parents constructor in an inherited class like that, you put it in the constructors initializer list:

Bus (string rego, string make, string model, int seats, int weight)
    : Vehicles(rego, make, model, seats, weight, "This is a bus record")
{ }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621