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