4
Hello, I have a question regarding classes in C++, I hope someone can help me. Thanks in advance!
I am developing a work for college where I need to register students, disciplines and grades and at 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 code of the Discipline 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
Cadastro Alunos
#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
Cadastro Notas
#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 there is a need to implement a function that by excluding a student from the student vector that is in the class Enrollment Students also have to delete all registered grades for this same student, but the grades are in a vector within another class, the Register Notes class. I can exclude the student and grades separately because in each class I have an exclude function, but from within the Student Register class I cannot delete the grades in the Register Grades class.
My function to delete the notes (It is in Notes.cpp where are my functions referring to the notes):
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 file Students.cpp in which I have my functions for Student Class and Student Registration, I can get the vector information but I can’t change it. If I call it inside the.cpp notes where are my functions referring to the notes it erases the notes correctly.
How should I proceed so that I can call from within my Students.cpp this function and it erases the data from the array of notes that is in the Register Grades class?
If you don’t understand anything or need more information and just say.
Thanks again!
EDIT >>>>>>>>>>>>>>>>>>>>>>>
I solved the problem as I commented below only that my "delete" function that I thought was 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);
}
The following happens:
In my note array, I have in each position the matricule, nameName, codeDiscipline, nameDiscipline, media1..., this function would have to scan the entire vector and where the matricule was equal to the searched one ("searchMatricula() function" just below) it would copy the information from the following positions a position above and decrease my index, and re-search if there is still another position with the matricula (I can have more of a registered discipline and registered note for each matricula).
It excludes all notes from the searched matricula, but the other matriculas get duplicate data.
For example I have 2 students/matriculas with 3 disciplines with registered grades for each one (6 positions of the occupied grade vector), if I exclude the student/enrollment 1 it excludes all information but the student/enrollment 2 gets two repeated grades and the last registered grade goes missing, and 4 positions are occupied where they should be only 3.
If anyone finds where I’m going wrong, I’ve made several changes but they all generate the same results or very similar.
Search License plate:
int CadastroNotas::pesquisarMatricula(int pos) {
for(int i = 0; i < indice; i++) {
if (notas[i].codigoMatricula == pos)
return i;
}
return -1;
}
EDIT 2 >>>>>>>>>>>>>>>>>>>>>
I solved the problem of the function "delete Otas()" after a lot of breaking the head; I think it is correct now (at least in my tests was working), if anyone finds any error comment q I beg to stay here as reference 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--;
}
}
}
Thank you all very much!!!
I was able to partially solve the problem, I changed my function from "void excluirAluno()" to "int excluirAluno()" returning me after the student deleted the number of the matricula and inside the file Menu.cpp where are all the menus I use in the program, in the reference case I called two functions to "excluirAluno()" which returns the matricula that I use in "excluirNotas(matricula)".
– Fabricio Andrade