mardi 19 janvier 2021

inherit Vehicle Class to a Template class

Vehicle header file

 #define vehicle_h

 #include "date.h"

 #include "storable.cpp"

 #include <string>
  using namespace std;
  typedef enum{bike = 1,car = 2,towera = 3} VehicleType;
  class Vehicle : public Storable
  {

  private:
    string registrationnumber;
    VehicleType type;
    int seats;
    string companyname;

    double pricePerKm;

    Date PUCExpirationDate;

  public:

    Vehicle(
    string registrationnumber,
    VehicleType type,
    int seats,
    string companyname,


    double pricePerKm,

    Date PUCExpirationDate, long recordID);


   string getregistrationnumber() const;


   VehicleType getVehicleType() const;

   string getVehicleTypeName() const;

  int getseats() const;

   string getcompanyName() const;

   double getPricePerKm() const;


   Date getPUCExpirationDate() const;


   void setPricePerKm(double newPrice);


   void display() const;

   string toString() const;

   void setDataForm (Storable * s);
  };



#endif // vehicle

Vehicle

#include "Vehicle.h"
#include "STRING_HELPER.h"

const char DELIMETER =';';

Vehicle :: Vehicle(string registrationnumber,VehicleType type,int seats,
                    string companyName , double pricePerKm , Date PUCExpirationDate , long recordID=0):Storable(recordID)
                    {

                        this->registrationnumber =registrationnumber;
                        this->type= type;
                        this->seats = seats;
                        this-> companyname = companyname;
                        this->pricePerKm = pricePerKm;
                        this->PUCExpirationDate = PUCExpirationDate;

                    }

                    string Vehicle ::getregistrationnumber() const
                    {

                        return this->registrationnumber;
                    }

                    VehicleType Vehicle::getVehicleType() const
                    {

                        return this->type;
                    }

                    int Vehicle::getseats() const
                    {

                        return this->seats;
                    }

                    string Vehicle::getcompanyName() const

                    {
                        return this->companyname;
                    }

                    double Vehicle::getPricePerKm() const
                    {
                        return this->pricePerKm;
                    }

                    Date Vehicle::getPUCExpirationDate() const
                    {

                        return this->PUCExpirationDate;
                    }
                    void Vehicle::setPricePerKm( double newprice)
                    {

                        this->pricePerKm= newprice;
                    }

                    string Vehicle::getVehicleTypeName() const
                    {

                        switch(this->type)
                        {

                        case VehicleType:: bike:
                            return "bike";
                        case VehicleType:: car:
                            return "car";
                        case VehicleType:: towera:
                            return "Tower";
                        default:
                            return " ";
                        }
                    }

                    void Vehicle::display() const
                    {


                        cout<< "Vehicle Details :"<<endl;
                        cout<< "Registration Number : "<< this->registrationnumber<<endl;

                        cout<< "Vehicle Type : "<< this->type<<endl;

                        cout<< "No of seats : "<< this->seats<<endl;

                        cout<< "Company Name : "<< this->companyname<<endl;

                        cout<< "Price PER Km : "<< this->pricePerKm<<endl;

                        cout<< "PUC ExpirationDate : "<< this->PUCExpirationDate.toString()<<endl;
                    }

                    string Vehicle::toString() const
                    {

                        stringstream ss;
                        ss<<recordID<< DELIMETER<< registrationnumber<< DELIMETER<< type<< DELIMETER<< seats<< DELIMETER<< companyname<< DELIMETER
                        << to_string(pricePerKm)<< DELIMETER<< PUCExpirationDate.toString();



                        return ss.str();
                    }

                    void Vehicle ::setDataForm(Storable *s)
                    {
                        Vehicle *v = dynamic_cast<Vehicle *> (s);
                        if(v)
                        {
                            this->registrationnumber= v->registrationnumber;
                            this->type= v->type;
                            this->companyname= v->companyname;
                            this->seats= v->seats;
                            this->pricePerKm = v->pricePerKm;
                            this->PUCExpirationDate= v->PUCExpirationDate;
                        }
                    }

Template Table header file



#define table_h
#include "storable.cpp"
#include "Vehicle.h"

#include "error.cpp"

#include <vector>
#include<string>
#include <fstream>

using namespace std;

template <class T> class Table {
private:
    string fileName;
    fstream fileStream;
    vector<Storable *> * records = NULL;

    T * getRefOfRecord(long recordID) const throw(IOError);

    void writeToFile () throw(IOError);
     T * addNewRecord (T data) const throw (IOError);

    void updateRecord (T updateRecord) const throw (IOError);


public:
    Table(string fileName) throw (MemoryError);
    ~Table();

    long getNextRecordId() const;




    const T* const getRecordForld(long recordId) const throw(IOError);

    friend class Database;





};

#endif // table_h

Template table .cpp


#include "Table.h"

#include<iostream>

using namespace std;

template <class T> Table<T> ::Table(string fileName)
throw(MemoryError)
{


    this->fileName = fileName;
    this->records = new vector<Storable *> ();


    if(!this->records)
    {

        throw MemoryError();
    }

}

template <class T> long Table<T> ::getNextRecordId() const
{

    return this->records->size()+1;
}


template <class T> T* Table<T> ::addNewRecord (T record) const throw (IOError)
{

    T *newRecord = new T(record);

    if(!newRecord)
    {

        throw new MemoryError();
    }

    newRecord->recordId = this->getNextRecordId();

    this->records->push_back(newRecord);

    try
    {
        this->writeToFile();
    }
    catch(IOError error)
    {

        this->records->pop_back();
        delete newRecord;
        throw;
    }
    return newRecord;
}




template <class T> void Table<T> ::updateRecord(T updateRecord) const throw(IOError)
{

    for(auto & record:*this->records)
    {

        if(record->getRecordID()== updateRecord.getRecordID())
        {
            T * ptr = dynamic_cast<T*> (record);
            if(ptr)
            {
                T oldRecord = T(* ptr);
                record->setDataFrom(&updateRecord);
                try
                {
                    this->writeToFile();
                    return;
                }
                catch(IOError error)
                {
                    record->setDataFrom (&oldRecord);
                    throw;
                }
            }
        }
    }
    throw MemoryError();
}


template <class T> void Table<T> ::writeToFile() throw (IOError)
{

    this->fileStream.open(fileName,ios::out | ios::trunc);

    if(!this->fileStream)
    {

        throw IOError();
    }
    for(auto & record: *records)
    {

        fileStream <<record->toString()<<endl;

    }

    this->fileStream.close();

}



template <class T> const T* const Table<T> ::getRecordForld(long recordId) const throw(IOError)
{

    try
    {
        return this->getRefOfRecord(recordId);
    }
    catch(IOError)
    {

        throw;
    }

}


template <class T> T* Table<T> ::getRefOfRecord(long recordId) const throw (IOError)
{


    for(auto &record :*records)
    {

        if(record->getRecordID()== recordId)
        {

            return dynamic_cast<T*> (record);
        }
    }
    throw IOError();

}

template <class T> Table<T> ::~Table()
{

    for(auto & record : *this->records)
    {

        delete dynamic_cast<T*> (record);
    }
    this->records->clear();

    this->records->shrink_to_fit();


    delete this->records;
}

the template class working fine with the other class such as user or trip but with vehicle class I am getting this error give the error invalid abstract parameter type 'Vehicle' I am stuck in this place two three day ago I terid but didn't find any clue !!

Aucun commentaire:

Enregistrer un commentaire