File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < iostream>
3+ using namespace std ;
4+
5+ // 人
6+ class Person {
7+ private:
8+ string m_strName;
9+ public:
10+ Person (string strName) {
11+ m_strName = strName;
12+ }
13+ Person () {}
14+ virtual void Show () {
15+ cout << " 裝扮的是:" << m_strName << endl;
16+ }
17+ };
18+
19+ // 裝飾類
20+ class Finery : public Person {
21+ protected:
22+ Person* m_component;
23+ public:
24+ void Decorate (Person* component) {
25+ m_component = component;
26+ }
27+ virtual void Show () {
28+ m_component->Show ();
29+ }
30+ };
31+
32+ // T 恤
33+ class TShirts : public Finery {
34+ public:
35+ virtual void Show () {
36+ cout << " T Shirts" << endl;
37+ m_component->Show ();
38+ }
39+ };
40+
41+ // 褲子
42+ class BigTrouser : public Finery {
43+ public:
44+ virtual void Show () {
45+ cout << " Big Trouser" << endl;
46+ m_component->Show ();
47+ }
48+ };
49+
50+ // 用戶端
51+ int main () {
52+ Person* p = new Person (" 小李" );
53+ BigTrouser* bt = new BigTrouser ();
54+ TShirts* ts = new TShirts ();
55+ bt->Decorate (p);
56+ ts->Decorate (bt);
57+ ts->Show ();
58+ return 0 ;
59+ }
60+
Original file line number Diff line number Diff line change 1+ class COperation {
2+ public:
3+ int m_nFirst;
4+ int m_nSecond;
5+ virtual double GetResult () {
6+ double dResult = 0 ;
7+ return dResult;
8+ }
9+ };
10+
11+ class AddOperation : public COperation {
12+ public:
13+ virtual double GetResult () {
14+ return m_nFirst + m_nSecond;
15+ }
16+ };
17+
You can’t perform that action at this time.
0 commit comments