1
2
3
4
5
6
7
8
9
10
11
| #include "CPU.h" #include "Motherboard.h" #include "RAM.h" class Computer { private : CPU m_cCPU; Motherboard m_cMotherboard; RAM m_cRAM1,m_cRAM2; }; |
1
2
3
4
| PersonalComputer::PersonalComputer( int nCPUSpeed, char *strMotherboardModel, int nRAMSize) : m_cCPU(nCPUSpeed), m_cMotherboard(strMotherboardModel), m_cRAM1(nRAMSize), { } |
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
| #ifndef POINT2D_H #define POINT2D_H #include <iostream> class Point2D { private : int m_nX; int m_nY; public : // A default constructor Point2D() : m_nX(0), m_nY(0) { } // A specific constructor Point2D( int nX, int nY) : m_nX(nX), m_nY(nY) { } // An overloaded output operator friend std::ostream& operator<<(std::ostream& out, const Point2D &cPoint) { out << "(" << cPoint.GetX() << ", " << cPoint.GetY() << ")" ; return out; } // Access functions void SetPoint( int nX, int nY) { m_nX = nX; m_nY = nY; } int GetX() const { return m_nX; } int GetY() const { return m_nY; } }; #endif |
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
| #ifndef CREATURE_H #define CREATURE_H #include <iostream> #include <string> #include "Point2D.h" class Creature { private : std::string m_strName; Point2D m_cLocation; // We don't want people to create Creatures with no name or location // so our default constructor is private Creature() { } public : Creature(std::string strName, const Point2D &cLocation) : m_strName(strName), m_cLocation(cLocation) { } friend std::ostream& operator<<(std::ostream& out, const Creature &cCreature) { out << cCreature.m_strName.c_str() << " is at " << cCreature.m_cLocation; return out; } void MoveTo( int nX, int nY) { m_cLocation.SetPoint(nX, nY); } }; #endif |
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
| #include <string> #include <iostream> #include "Creature.h" int main() { using namespace std; cout << "Enter a name for your creature: " ; std::string cName; cin >> cName; Creature cCreature(cName, Point2D(4, 7)); while (1) { cout << cCreature << endl; cout << "Enter new X location for creature (-1 to quit): " ; int nX=0; cin >> nX; if (nX == -1) break ; cout << "Enter new Y location for creature (-1 to quit): " ; int nY=0; cin >> nY; if (nY == -1) break ; cCreature.MoveTo(nX, nY); } return 0; } |
- Each individual class can be kept relatively simple and straightforward, focused on performing one task. This makes those classes easier to write and much easier to understand. For example, Point2D only worries about point-related stuff, which helps keep it simple.
- Each subobject can be self-contained, which makes them reusable. For example, we could reuse our Point2D class in a completely different application. Or if our creature ever needed another point (for example, a destination it was trying to get to), we can simply add another Point2D member variable.
- The complex class can have the simple subclasses do most of the hard work, and instead focus on coordinating the data flow between the subclasses. This helps lower the overall complexity of the complex object, because it can delegate tasks to the sub-objects, who already know how to do them. For example, when we move our Creature, it delegates that task to the Point class, which already understands how to set a point. Thus, the Creature class does not have to worry about how such things would be implemented.
0 comments: