(原創) 如何使用pointer和reference達成Polymorphism? (C/C++)
C++有三種物件表示方式:object, pointer, reference,C#只有object很單純,但對於最重要的多型,C++不能用object表示,這會造成object slicing,必須用pointer和reference達成,若要將多型的object放進container,則一定得用pointer,因為reference不能copy,這也是C++另外兩個一定得用pointer的地方。
本範例demo如何使用pointer和reference達成多型。
1/*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : PolymorphismPointerReference.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use pointer & reference for polymorphism
7Release : 03/20/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Student {
15public:
16 string name;
17
18protected:
19 Student() {}
20 Student(const char *name) : name(string(name)) {}
21
22public:
23 virtual string job() const = 0;
24};
25
26class Bachelor : public Student {
27public:
28 Bachelor();
29 Bachelor(const char *name) : Student(name) {}
30
31public:
32 string job() const {
33 return "study";
34 }
35};
36
37class Master : public Student {
38public:
39 Master() {}
40 Master(const char *name) : Student(name) {}
41
42public:
43 string job() const {
44 return "study, research";
45 }
46};
47
48int main() {
49 // C# : Student John = new Bachelor("John");
50 // use pointer
51 Student *John = &Bachelor("John");
52 cout << John->job() << endl;
53
54 // use reference
55 Student &Jack = Bachelor("Jack");
56 cout << Jack.job() << endl;
57
58 // C# : Student Mary = new Master("Mary");
59 Student *Mary = &Master("Mary");
60 cout << Mary->job() << endl;
61}
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : PolymorphismPointerReference.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use pointer & reference for polymorphism
7Release : 03/20/2007 1.0
8*/
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14class Student {
15public:
16 string name;
17
18protected:
19 Student() {}
20 Student(const char *name) : name(string(name)) {}
21
22public:
23 virtual string job() const = 0;
24};
25
26class Bachelor : public Student {
27public:
28 Bachelor();
29 Bachelor(const char *name) : Student(name) {}
30
31public:
32 string job() const {
33 return "study";
34 }
35};
36
37class Master : public Student {
38public:
39 Master() {}
40 Master(const char *name) : Student(name) {}
41
42public:
43 string job() const {
44 return "study, research";
45 }
46};
47
48int main() {
49 // C# : Student John = new Bachelor("John");
50 // use pointer
51 Student *John = &Bachelor("John");
52 cout << John->job() << endl;
53
54 // use reference
55 Student &Jack = Bachelor("Jack");
56 cout << Jack.job() << endl;
57
58 // C# : Student Mary = new Master("Mary");
59 Student *Mary = &Master("Mary");
60 cout << Mary->job() << endl;
61}
執行結果
study
study
study, research
study
study, research
49行和58行為C#的寫法,使用object即可,但若用C++,51行為pointer寫法,55行為reference寫法,但不能使用object寫法。
See Also
(原創) 什麼是物件導向(Object Oriented)? (初級) (C/C++/C#)
(原創) 何時該使用object? 何時該使用reference? 何時該使用pointer? (初級) (C++) (OO C++)