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