1 ///////////////////////////////////////////////////////////
2 // Softwarebauelemente II, Aufgabe O3.2
3 //
4 // author: Stephan Brumme
5 // last changes: May 29, 2001
6
7 #include "Room.h"
8 #include <iostream>
9
10
11 // default constructor
12 CRoom::CRoom()
13 {
14 m_nArea = 0;
15 m_nNumberOfRoom = 0;
16 }
17
18 // copy constructor
19 CRoom::CRoom(const CRoom &room)
20 {
21 operator=(room);
22 }
23
24 CRoom::CRoom(int nNumberOfRoom, int nArea)
25 {
26 m_nArea = nArea;
27 m_nNumberOfRoom = nNumberOfRoom;
28 }
29
30
31 // show attributes
32 string CRoom::Show() const
{
33 // check invariance
34 if (!ClassInvariant())
35 return "";
36
37 ostringstream strOutput;
38
39 strOutput << "Room no. " << m_nNumberOfRoom
40 << " covers an area of " << m_nArea << " square feet." << endl;
41
42 return strOutput.str();
43 }
44
45
46 // display the attributes
47 // only for internal purposes !
48 string CRoom::ShowDebug() const
{
49 ostringstream strOutput;
50
51 strOutput << "DEBUG info for 'CRoom'" << endl
52 << " m_nArea = " << m_nArea
<< " m_nNumberOfRoom = " << m_nNumberOfRoom << endl;
53
54 return strOutput.str();
55 }
56
57
58 // verify invariance
59 bool CRoom::ClassInvariant() const
{
60 // only positive numbers allowed
61 return (m_nArea > 0 && m_nArea <= MAXAREA &&
62 m_nNumberOfRoom > 0 && m_nNumberOfRoom <= MAXNUMBER);
63 }
64
65
66 // copy one room to another one
67 // prevents user from copying an object to itself
68 CRoom& CRoom::operator=(const CRoom &room)
69 {
70 // copy all variables
71 m_nArea = room.m_nArea;
72 m_nNumberOfRoom = room.m_nNumberOfRoom;
73
74 return *this;
75 }
76
77
78 // virtual, see operator=
79 bool CRoom::Copy(const CBasicClass* pClass)
80 {
81 // cast to CRoom
82 const CRoom *room = dynamic_cast<const CRoom*>(pClass);
83
84 // invalid class (is NULL when pClass is not a CRoom)
85 if (room == NULL || room == this)
86 return false;
87
88 // use non virtual reference based copy
89 // return value isn't needed
90 operator=(*room);
91
92 // we're done
93 return true;
94 }
95
96
97 // compare the room with another one
98 bool CRoom::operator==(const CRoom &room) const
{
99 return ((room.m_nArea == m_nArea) &&
100 (room.m_nNumberOfRoom == m_nNumberOfRoom));
101 }
102
103
104 // virtual, see operator==
105 bool CRoom::EqualValue(const CBasicClass* pClass) const
{
106 // cast to CRoom
107 const CRoom *room = dynamic_cast<const CRoom*>(pClass);
108
109 // invalid class (is NULL when pClass is not a CRoom)
110 if (room == NULL || room == this)
111 return false;
112
113 // use non virtual reference based copy
114 return operator==(*room);
115 }
116
117
118 // retrieve the private value of m_nNumberOfRoom
119 int CRoom::GetNumberOfRoom() const
{
120 return m_nNumberOfRoom;
121 }
122
123
124 // change private m_nNumberOfRoom
125 void CRoom::SetNumberOfRoom(const int nNumberOfRoom)
126 {
127 m_nNumberOfRoom = nNumberOfRoom;
128 }
129
130
131 // retrieve the private value of m_nArea
132 int CRoom::GetArea() const
{
133 return m_nArea;
134 }
135
136