/////////////////////////////////////////////////////////// // Softwarebauelemente II, Aufgabe O3.2 // // author: Stephan Brumme // last changes: May 29, 2001 #include "Room.h" #include // default constructor CRoom::CRoom() { m_nArea = 0; m_nNumberOfRoom = 0; } // copy constructor CRoom::CRoom(const CRoom &room) { operator=(room); } CRoom::CRoom(int nNumberOfRoom, int nArea) { m_nArea = nArea; m_nNumberOfRoom = nNumberOfRoom; } // show attributes string CRoom::Show() const { // check invariance if (!ClassInvariant()) return ""; ostringstream strOutput; strOutput << "Room no. " << m_nNumberOfRoom << " covers an area of " << m_nArea << " square feet." << endl; return strOutput.str(); } // display the attributes // only for internal purposes ! string CRoom::ShowDebug() const { ostringstream strOutput; strOutput << "DEBUG info for 'CRoom'" << endl << " m_nArea = " << m_nArea << " m_nNumberOfRoom = " << m_nNumberOfRoom << endl; return strOutput.str(); } // verify invariance bool CRoom::ClassInvariant() const { // only positive numbers allowed return (m_nArea > 0 && m_nArea <= MAXAREA && m_nNumberOfRoom > 0 && m_nNumberOfRoom <= MAXNUMBER); } // copy one room to another one // prevents user from copying an object to itself CRoom& CRoom::operator=(const CRoom &room) { // copy all variables m_nArea = room.m_nArea; m_nNumberOfRoom = room.m_nNumberOfRoom; return *this; } // virtual, see operator= bool CRoom::Copy(const CBasicClass* pClass) { // cast to CRoom const CRoom *room = dynamic_cast(pClass); // invalid class (is NULL when pClass is not a CRoom) if (room == NULL || room == this) return false; // use non virtual reference based copy // return value isn't needed operator=(*room); // we're done return true; } // compare the room with another one bool CRoom::operator==(const CRoom &room) const { return ((room.m_nArea == m_nArea) && (room.m_nNumberOfRoom == m_nNumberOfRoom)); } // virtual, see operator== bool CRoom::EqualValue(const CBasicClass* pClass) const { // cast to CRoom const CRoom *room = dynamic_cast(pClass); // invalid class (is NULL when pClass is not a CRoom) if (room == NULL || room == this) return false; // use non virtual reference based copy return operator==(*room); } // retrieve the private value of m_nNumberOfRoom int CRoom::GetNumberOfRoom() const { return m_nNumberOfRoom; } // change private m_nNumberOfRoom void CRoom::SetNumberOfRoom(const int nNumberOfRoom) { m_nNumberOfRoom = nNumberOfRoom; } // retrieve the private value of m_nArea int CRoom::GetArea() const { return m_nArea; }