0

The first problem: i want to create a array of object pointers to the class Material with size of 30

Main class

    #include <iostream>
#include <fstream>
#include "Material.h"
#include <string>
#include <sstream>

using namespace std;

Material *materiales = new Material[30];


int cantMateriales;

void cargarMateriales(){
    string tituloMaterial, datoCuatro, datoCinco, cadena, tipo;
    int idMaterial, version;
    ifstream inFile;
    inFile.open("Material.txt");
    cantMateriales = 0;
    while(getline(inFile, cadena)){

        stringstream idMaterialCast(cadena.substr(0, cadena.find(' ')));
        idMaterialCast >> idMaterial;
        materiales[cantMateriales].setIdMaterial(idMaterial);
        cadena = cadena.substr(cadena.find(' ') + 1, cadena.length() - 1);

        tituloMaterial = cadena.substr(0, cadena.find(' '));
        materiales[cantMateriales].setTitulo(tituloMaterial);
        cadena = cadena.substr(cadena.find(' ') + 1, cadena.length() - 1);

        tipo = cadena.substr(0, cadena.find(' '));
        cadena = cadena.substr(cadena.find(' ') + 1, cadena.length() - 1);
        if(tipo == "S"){
            Software* softwareP = static_assert<Software*>(materiales);
            //Version
            stringstream versionCast(cadena.substr(0, cadena.find(' ')));
            versionCast >> version;
            cadena = cadena.substr(cadena.find(' ') + 1, cadena.length() - 1);

            softwareP->setSo(cadena);
            softwareP->setVersion(version);
            softwareP->setIdMaterial(idMaterial);
            softwareP->setTitulo(tituloMaterial);

        }
        cout <<"Id de material: " << materiales[cantMateriales].getIdMaterial();
        cantMateriales++;
    }

    inFile.close();
}

void consultaMateriales(){

}

void consultarReservaciones(){
    ifstream inFile;
    inFile.open("Reserva.txt");
    if(inFile.peek() == ifstream::traits_type::eof()){
        //el archivo esta vacio, anunciar que esta vacio
        cout <<"No hay reservaciones"<<endl;
    }
    else{
        //Si no esta vacio, imprimelo

    }
    inFile.close();
}

char menu(){
    system("CLS");
    char opcion;
    cout <<"A.- Consultar la lista de Materiales"<<endl;
    cout <<"B.- Consultar la lista de Reservaciones"<<endl;
    cout <<"C.- Consultar las reservaciones de un material dado"<<endl;
    cout <<"D.- Consultar las reservaciones de una fecha dada"<<endl;
    cout <<"E.- Hacer una reservacion"<<endl;
    cout <<"F.- Salir"<<endl;
    cout <<"Seleccion: ";
    cin >> opcion;
    if(opcion == '\0'){
        menu();
    }
    else{
        return opcion;
    }
}

int main(){
    char seleccion;
    do{
        seleccion = menu();
            switch(seleccion){
            case 'A':
                    cargarMateriales();
                    system("pause");
                break;
            case 'B':
                    consultarReservaciones();
                    system("pause");
                break;
            case 'F':
                exit(1);
                break;
        }
    }while(seleccion != 'F');
}

Second problem, the class Material has 3 derived classes, I want to take information from a file (solved) and store the infor into the Material class and one of its derived classes (There is and if that will define if th info goes to Disco, Libro or Software)

the Material Class is this

  #include <iostream>
#include <string>
class Disco;
class Software;
class Libro;

using namespace std;

class Material{
    public:
    Material(){
        this->idMaterial = 0;
        this->titulo = "";
    }

    Material (int idMaterial, string titulo){
        this->idMaterial = idMaterial;
        this->titulo = titulo;
    }

    //Gettes, Metodos de acceso
    int getIdMaterial(){
        return this->idMaterial;
    }

    string getTitulo(){
        return this->titulo;
    }

    //Settes, Metodos de modificacio
    void setIdMaterial(int idMaterial){
        this->idMaterial = idMaterial;
    }

    void setTitulo(string titulo){
        this->titulo = titulo;
    }

    private:
    int idMaterial;
    string titulo;

};

So the real question is: How can I declare a 30 space object pointer variable and then populate the Base class (Material) and one of its derived classes (Libro, Software or Disco)

Krishna Kanth Yenumula
  • 2,390
  • 2
  • 10
  • 21

1 Answers1

0

Maybe I am misunderstanding the question but here goes: Since it sounds like you already know how many elements you need, you may want to instead create your array off the stack rather than heap (or a vector could work too). You should make this an array of pointers to Material objects. So something like this

Material *materiales[30];

Using your conditionals where you mentioned it will determine if Disco, Libro, or Software, do something like this:

materiales[someIndex] = new Software(args, _for, some, constructor);

Then in your Software constructor you can call the parent class and provide it the appropriate args

If you take this approach, you will need to release all allocated memory at the end of your program.

v.p.
  • 78
  • 5
  • Is there a way to populate BASE class and then derived class? Like, in a separate way? – Luis Garcia Nov 24 '20 at 05:04
  • With your set up it looks like you can since all of your member functions in your Material Class are public. If you are using inheritance then you should be able to just say something like `new Software()` and then call the parent functions like normal. You may have to static_cast<> to Software in order to call the Software member functions, but I haven't tried your code locally – v.p. Nov 24 '20 at 05:11
  • Thank you a lot man! Do you know how can I access every Material and its respective derived class (Disco, Libro or Software) to print them? – Luis Garcia Nov 24 '20 at 05:13
  • I've got "invalid use of incomplete type 'Class Software'" – Luis Garcia Nov 24 '20 at 05:19
  • Do you know how can I access that initialized "materiales[someIndex] = new Software(args, _for, some, constructor);" to print the args I passed before? – Luis Garcia Nov 24 '20 at 05:59