Hello, I have a question regarding classes in C ++, I hope someone can help me. Thanks in advance!
I'm developing a college assignment where I need to enroll students, subjects, and grades, and in the end display some reports, all using the concept of classes and object orientation. The program is 99% ready I only found problems to implement the following:
I have 6 classes implemented (I will not post the Discipline code because it is not the case):
Students
#ifndef ALUNOS_HPP_INCLUDED
#define ALUNOS_HPP_INCLUDED
#include "Bibliotecas.hpp"
class Aluno {
public:
Aluno();
string nome;
string cpf;
string bairro;
string cidade;
string endereco;
string identidade;
string estadoCivil;
string dataNascimento;
int numeroMatricula;
};
#endif // ALUNOS_HPP_INCLUDED
Student Registration
#ifndef CADASTROALUNOS_HPP_INCLUDED
#define CADASTROALUNOS_HPP_INCLUDED
#include "Alunos.hpp"
#include "CadastroNotas.hpp"
//class CadastroNotas;
class CadastroAlunos {
public:
CadastroNotas cadNotas;
Aluno alunos[100];
int indice;
CadastroAlunos() {indice = 0;}
void cadastrarAlunos();
void alterarAlunos();
void excluirAlunos(CadastroNotas cadNotas);
void listarAlunos();
int pesquisar(int numMatricula);
};
#endif // CADASTROALUNOS_HPP_INCLUDED
Notes
#ifndef NOTAS_HPP_INCLUDED
#define NOTAS_HPP_INCLUDED
#include "Bibliotecas.hpp"
class Nota {
public:
Nota();
int codigoNota;
int codigoMatricula;
int codigoDisciplina;
double media1;
double media2;
double media3;
double media4;
double mediaFinal;
string nomeAluno;
string nomeDisciplina;
};
#endif // NOTAS_HPP_INCLUDED
Registration Notes
#ifndef CADASTRONOTAS_HPP_INCLUDED
#define CADASTRONOTAS_HPP_INCLUDED
#include "Notas.hpp"
//#include "CadastroAlunos.hpp"
#include "CadastroDisciplinas.hpp"
class CadastroAlunos;
class CadastroNotas {
public:
//CadastroAlunos cadAlunos;
CadastroDisciplinas cadDisciplinas;
Nota notas[100];
int indice;
CadastroNotas() {indice = 0;}
void cadastrarNotas(CadastroAlunos cadAlunos, CadastroDisciplinas cadDisciplinas);
void alterarNotas();
void excluirNotas();
void listarNotas();
void excluirNotas(int numMatricula);
int pesquisarMatricula(int pos);
int pesquisarDisciplina(int pos);
int comparaPosicao(int matricula, int disciplina);
string aprovado_reprovado(int pos);
};
#endif // CADASTRONOTAS_HPP_INCLUDED
In them I declare my vectors, the variables I have within each vector and the prototypes of the functions.
The program works 100%, but it is necessary to implement a function that when deleting a student from the student vector that is in the Student Master class, I also have to delete all the notes registered for this same student, but the notes are in a vector inside of another class, the Notes Master class. I can exclude the student and the notes separately because in each class I have a delete function, but from within the Student Master class I can not delete the notes in the Master Notes class.
My function to delete notes (It is in Notes.cpp where my notes functions are):
void CadastroNotas::excluirNotas(int numMatricula) {
int pos = 0;
do {
pos = pesquisarMatricula(numMatricula);
if(pos >= 0){
for(int i = pos; i < indice; i++) {
notas[pos] = notas[pos + 1];
indice--;
}
}
} while(pos >= 0);
}
I call this function in my Student.cpp file where I have my functions for Student and Student Class, I can get the information from the vector but I can not change it. If I call it inside the Notes.cpp where my notes functions are, it erases the notes correctly.
How do I get this function to call from within my Student.cpp and delete it from the note vector that is in the Notes Master class?
If you do not understand something or need more information and just say.
Thanks again!
EDIT> EDIT> EDIT ; >
I solved the problem as I mentioned below only that my "deleteNotes ()" function that I found to be working correctly is not.
void CadastroNotas::excluirNotas(int numMatricula) {
int pos = 0;
do {
pos = pesquisarMatricula(numMatricula);
if(pos >= 0){
for(int i = pos; i < indice; i++) {
notas[pos] = notas[pos + 1];
}
indice--;
}
} while(pos >= 0);
}
It happens as follows:
In my vector of notes I have in each position the registration, nameAluno, codeDisciplina, nombreDisciplina, media1 ..., this function would have to scan the entire vector and where the registration was equal to the searched one (function "searchMatricula ()" logo below) would copy the information from the following positions to a position above and would decrease my index, and I would re-search if there is still another position with the enrollment (I can have more than one registered subject and one registered note for each enrollment).
It excludes all notes from the enrolled enrollment, but other enrollments have duplicate data.
For example, I have 2 students with 3 subjects with notes registered for each one (6 positions of the vector of occupied notes), if I have to exclude the student 1, he excludes all the information but the student 2 with two repeated notes and the last recorded note goes out, and there are 4 occupied positions where they should be only 3.
If someone finds where I am wrong, I have made several changes but all generate the same or very similar results.
Search Enrollment:
int CadastroNotas::pesquisarMatricula(int pos) {
for(int i = 0; i < indice; i++) {
if (notas[i].codigoMatricula == pos)
return i;
}
return -1;
}
EDIT 2> EDIT 2> EDIT 2
I solved the problem of the "deleteNotes ()" function after a lot of head breaking; I think it's correct now (at least in my tests it was working), if anyone finds any errors, I'll comment on them for future research.
void CadastroNotas::excluirNotas(int numMatricula) {
for(int i = 0; i < indice; i++) {
if(notas[i].codigoMatricula == numMatricula) {
for(int j = i; j < indice; j++) {
notas[j] = notas[j + 1];
}
i--;
indice--;
}
}
}
Many thanks to all!