[ create a new paste ] login | about

Link: http://codepad.org/hOwJ1LQc    [ raw code | output | fork ]

C++, pasted on May 26:
#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 < m_nClassSize; 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 < m_nClassSize; 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)
     {
        delete [] input.m_pStudent;
        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);
}


Output:
1
2
3
4
5
Line 20: error: teacher.h: No such file or directory
Line 26: error: leakdetection.h: No such file or directory
Line 22: error: classroom.h: No such file or directory
Line 15: error: 'CClassroom' has not been declared
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: