Data Structure, Dynamic Memory Allocation in C++ and Data Reading

Asked

Viewed 77 times

1

I have a code with encapsulation and composition with 3 classes in C++. In my code I have to display objects in a static way but I want to create them dynamically. I have a class called worker that has 2 objects from other 2 classes (info and family) as its members, so worker has info and family. Every time I want to create a working object (which has info and family) I have to do it manually (static) but I want to do it dynamically. I want the user to choose how many workers he wants to create dynamically. I think I should use data structures as a list and dynamic memory allocation but I don’t know how to do it. My code also creates a file called "cadastro.dat" and I want to read this file but also do not know how to do it. Could someone help me? I’m showing you the code I’ve already made.

//MAIN
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::ios;

#include <fstream> 
using std::ofstream;

#include <cstdlib>
using std::exit;

#include "trabalhador.h"

int main()
{
    ofstream saidaArquivoCadastro( "cadastro.dat", ios::out );
    
    if ( !saidaArquivoCadastro ) // operador ! sobrecarregado
 {
 cerr << "Arquivo nao pode ser aberto" << endl;
 exit( 1 );
 }
 
    cout << "INFORME OS DADOS SOLICITADOS: \n\n";
    cout << "Nome do funcionario: ";
    info i1;
    cout << "\nCPF: ";
    info i2;
    cout << "\nNome da mae: ";
    familia f1;
    cout << "\nNome do pai: ";
    familia f2;
    trabalhador t(i1, i2, f1, f2);
    
    return 0;
}

//trabalhador.h
#ifndef TRABALHADOR_H
#define TRABALHADOR_H

#include "info.h"
#include "familia.h"

class trabalhador
{
    public:
        trabalhador (const info &, const info &, const familia &, const familia &);
        void print () const;
    private:
        const info funcionarioNome;
        const info funcionarioCPF;
        const familia mae;
        const familia pai;
};

#endif

//trabalhador.cpp
#include <iostream>
using std::cout;

#include "trabalhador.h"
#include "info.h"

trabalhador::trabalhador (const info &infoNome, const info &infoCPF,
const familia &famiMae, const familia &famiPai)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF),
mae (famiMae),
pai (famiPai)
{
    cout << "\nDados do funcionario: \n";
    print();
}

void trabalhador::print() const
{
    funcionarioNome.print();
    funcionarioCPF.print();
    mae.print();
    pai.print();
}

//info.h
#include <string>
using std::string;

#ifndef INFO_H
#define INFO_H

class info
{
    public:
        info (string = "");
        void setInfoDado (string);
        void setInfo ();
        void print () const;
    private:
        string infoDado;
};

#endif

//info.cpp
#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::string;
using std::getline;

#include "info.h"

info::info (string info)
{
    setInfoDado (info);
}

void info::setInfoDado (string info)
{
    infoDado = info;
    setInfo();
}

void info::setInfo ()
{   
    string nome;
    getline (cin, nome);
    infoDado = nome;
}

void info::print () const
{
    cout << infoDado << "\n";
}

//familia.h
#include <string>
using std::string;

#ifndef FAMILIA_H
#define FAMILIA_H

class familia
{
    public:
        familia (string = "");
        void setFamiDado (string);
        void setFami ();
        void print () const;
    private:
        string famiDado;
};

#endif

//familia.cpp
#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::string;
using std::getline;

#include "familia.h"

familia::familia (string info)
{
    setFamiDado (info);
}

void familia::setFamiDado (string info)
{
    famiDado = info;
    setFami();
}

void familia::setFami ()
{   
    string nome;
    getline (cin, nome);
    famiDado = nome;
}

void familia::print () const
{
    cout << famiDado << "\n";
}

1 answer

0

In my code I have to display objects in a static way but I want create them dynamically. I have a class called worker that has 2 objects from other 2 classes (info and family) like yours members, so that worker has info and family. Every time I I want to create a worker object (which has info and family) I have to do it manually (static) but I want to do it dynamically

The biggest problem with your code is that you missed the constructs for info and família and ended up without the implementation of the default constructor of these classes. And in the case of the class trabalhador not only did not write a standard constructor but still wrote a single constructor that creates 2 instances of familia and two of info, except that without the constructor you imagined you would read the keyboard values to the only field of these classes. Behold:

class trabalhador
{
    public:
        trabalhador (const info &, const info &, const familia &, const familia &);
        void print () const;
    private:
        const info funcionarioNome;
        const info funcionarioCPF;
        const familia mae;
        const familia pai;
};

I could have written the normal way

class trabalhador
{
public:
    trabalhador() :
        funcionarioNome(info()),
        funcionarioCPF(info()),
        mae(familia()),
        pai(familia())
    {};
        
    trabalhador(
        const info&,
        const info&,
        const familia&,
        const familia&
    );
    void print() const;

private:
    const info funcionarioNome;
    const info funcionarioCPF;
    const familia mae;
    const familia pai;
};

And then there would be the two builders. And to create some workers could write the normal:

    trabalhador* WorkForce = new trabalhador[20];
    WorkForce[19].print();
    delete[] WorkForce;

Note that the way you wrote the 3 classes can do nothing with it except call print() for the worker.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.