Alright so the project itself is almost done, I just need to alter some of the coding with whatever help I get from you guys.
I will give you guys 2 files, one is a header file and the other is the code file. The header file is just so you guys can see the members of the class, and how I should change my code accordingly.
Now, I should mention that my code compiles just fine, but I've set up tests along the way to see if it would have any problems, and there seems to be a problem with the way I'm trying to use my Database class, because this is an error I'm getting in the console:
1 2 3
|
GetNumClasses returned 1 but that's wrong!
Error adding the class to the database.
Database test failed.
|
And I'm also not sure how to handle integer arrays with the copy constructor, as you'll see.
Anyway, here are the files:
classroom.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#ifndef CLASSROOM_H
#define CLASSROOM_H
#include <cstring>
#include "defines.h"
#include <iostream>
using namespace std;
class CClassroom
{
char *m_pName;
int *m_pStudent;
int m_nTeacherID;
int m_nClassSize;
public:
CClassroom();
CClassroom(const CClassroom& input);
CClassroom(int size);
~CClassroom();
int GetStudentEnrolled(int index);
bool IsStudentEnrolled(int studentID);
void SetStudentEnrolled(int index);
char* GetClassName() {return m_pName;}
void GetClassName(char input[]);
void SetName(const char *input){strcpy(m_pName, input);}
double GetClassSize();
void SetClassSize(int nClassSize);
double GetTeacherID();
void SetTeacher(int input) { m_nTeacherID = input;}
void AddStudent(int nStudentID);
CClassroom& operator=(const CClassroom& rhs);
friend ostream& operator<<(ostream& os,
const CClassroom& input);
friend istream& operator>>(istream& is, CClassroom &input);
friend bool operator==(const CClassroom& lhs, const CClassroom &rhs);
friend bool operator!=(const CClassroom& lhs, const CClassroom &rhs);
};
inline int CClassroom::GetStudentEnrolled(int index)
{
return m_pStudent[index];
}
inline void CClassroom::SetStudentEnrolled(int index)
{
m_pStudent[index] = index;
}
inline double CClassroom::GetClassSize()
{
return m_nClassSize;
}
inline void CClassroom::SetClassSize(int input)
{
m_nClassSize = input;
}
inline double CClassroom::GetTeacherID()
{
return m_nTeacherID;
}
#endif
|
And classroom.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "teacher.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include "leakdetection.h"
#include "classroom.h"
#ifdef MEMORY_LEAK_DETECTION
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
#endif
using namespace std;
CClassroom::CClassroom()
{
m_nClassSize = 0;
m_pName = new char[MAX_CLASS_NAME_LENGTH];
m_pStudent = new int[MAX_CLASS_SIZE];
}
CClassroom::CClassroom(const CClassroom& input)
{
m_nClassSize = input.m_nClassSize;
m_nTeacherID = input.m_nTeacherID;
m_pName = new char[MAX_CLASS_NAME_LENGTH];
strcpy(m_pName, input.m_pName);
m_pStudent = new int[MAX_CLASS_SIZE];
for (int i = 0; i < MAX_CLASS_SIZE; i++)
{
m_pStudent[i] = input.m_pStudent[i];
}
}
CClassroom::CClassroom(int size)
{
m_pName = NULL;
m_pStudent = NULL;
m_nClassSize = size;
}
void CClassroom::GetClassName(char input[])
{
strcpy(input, m_pName);
}
bool CClassroom::IsStudentEnrolled(int studentID)
{
for (int i = 0; i < m_nClassSize; i++)
{
if (studentID == m_pStudent[i])
{
return true;
}
}
return false;
}
void CClassroom::AddStudent(int nStudentID)
{
for (int i = 0; i < m_nClassSize; i++)
{
if (m_pStudent[i] == NULL)
{
m_pStudent[i] == nStudentID;
}
}
}
ostream& operator<<(ostream& os, const CClassroom& input)
{
os << input.m_pName
<< endl
<< input.m_nTeacherID
<< endl
<< input.m_nClassSize
<< endl;
for (int i = 0; i < input.m_nClassSize; i++)
{
os << input.m_pStudent[i]<< " ";
}
return os;
}
CClassroom& CClassroom::operator=(const CClassroom& rhs)
{
if(&rhs == this)
{
return *this;
}
delete [] m_pName;
delete [] m_pStudent;
m_nClassSize = rhs.m_nClassSize;
m_pName = new char[MAX_NAME_LENGTH];
m_nTeacherID = rhs.m_nTeacherID;
strcpy(m_pName, rhs.m_pName);
m_pStudent = new int[MAX_CLASS_SIZE];
for (int i = 0; i < MAX_CLASS_SIZE; i++)
{
m_pStudent[i] = rhs.m_pStudent[i];
}
return *this;
}
istream& operator>>(istream& is, CClassroom& input)
{
if(input.m_pName == NULL)
{
input.m_pName = new char[MAX_NAME_LENGTH];
}
if(input.m_pStudent == NULL)
{
input.m_pStudent = new int[MAX_CLASS_SIZE];
}
is.getline(input.m_pName, MAX_NAME_LENGTH);
is >> input.m_nTeacherID >> input.m_nClassSize;
for (int i = 0; i < input.m_nClassSize; i++)
{
is >> input.m_pStudent[i];
}
is.ignore();
return is;
}
CClassroom::~CClassroom()
{
delete [] m_pName;
delete [] m_pStudent;
}
bool operator==(const CClassroom& lhs, const CClassroom &rhs)
{
// check if either is null, we shouldn't compare strings
// using strcmp if they're null
if(rhs.m_pName == NULL || lhs.m_pName == NULL)
{
if(rhs.m_pName == NULL && lhs.m_pName == NULL &&
(lhs.m_nTeacherID == rhs.m_nTeacherID) &&
(lhs.m_nClassSize == rhs.m_nClassSize) &&
(lhs.m_pStudent == rhs.m_pStudent))
{
return true;
}
else
{
return false;
}
}
if((lhs.m_nTeacherID != rhs.m_nTeacherID) ||
(lhs.m_nClassSize != rhs.m_nClassSize) ||
strcmp(rhs.m_pName, lhs.m_pName) ||
(lhs.m_pStudent != rhs.m_pStudent))
{
return false;
}
return true;
}
bool operator!=(const CClassroom& lhs, const CClassroom &rhs)
{
return !(lhs == rhs);
}
|
My concerns are with the following methods:
- Classroom()
- CClassroom(const CClassroom& input)
- CClassroom(int size)
- IsStudentEnrolled(int studentID)
- AddStudent(int nStudentID)
- operator=(const CClassroom& rhs)
- operator>>(istream& is, CClassroom& input)
Also, this is the code snippet where my program is failing from (the test portion of it)
1 2 3 4 5 6 7
|
if(classData.GetNumClasses() != 10)
{
cout << "GetNumClasses returned " << classData.GetNumClasses() << " but that's wrong! \n";
cout << "Error adding the class to the database" << endl;
return false;
}
|
And this is the code it's running in order for me to get that error:
http://codepad.org/ceyJNvnc
I can post the entire project if need be, but considering how big it is I'll have to paste it on codepad and link you guys..
Anyway, thank you in advance!