(原創) 如何使用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
4
Filename : PolymorphismPointerReference.cpp
5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6
Description : Demo how to use pointer & reference for polymorphism
7
Release : 03/20/2007 1.0
8
*/
9
#include <iostream>
10
#include <string>
11
12
using namespace std;
13
14
class Student {
15
public:
16
string name;
17
18
protected:
19
Student() {}
20
Student(const char *name) : name(string(name)) {}
21
22
public:
23
virtual string job() const = 0;
24
};
25
26
class Bachelor : public Student {
27
public:
28
Bachelor();
29
Bachelor(const char *name) : Student(name) {}
30
31
public:
32
string job() const {
33
return "study";
34
}
35
};
36
37
class Master : public Student {
38
public:
39
Master() {}
40
Master(const char *name) : Student(name) {}
41
42
public:
43
string job() const {
44
return "study, research";
45
}
46
};
47
48
int 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.com3

4
Filename : PolymorphismPointerReference.cpp5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++6
Description : Demo how to use pointer & reference for polymorphism7
Release : 03/20/2007 1.08
*/9
#include <iostream>10
#include <string>11

12
using namespace std;13

14
class Student {15
public:16
string name;17
18
protected:19
Student() {}20
Student(const char *name) : name(string(name)) {}21

22
public:23
virtual string job() const = 0;24
};25

26
class Bachelor : public Student {27
public:28
Bachelor();29
Bachelor(const char *name) : Student(name) {}30

31
public:32
string job() const {33
return "study";34
}35
};36

37
class Master : public Student {38
public:39
Master() {}40
Master(const char *name) : Student(name) {}41
42
public:43
string job() const {44
return "study, research";45
}46
};47

48
int main() {49
// C# : Student John = new Bachelor("John");50
// use pointer51
Student *John = &Bachelor("John");52
cout << John->job() << endl;53
54
// use reference55
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, research49行和58行為C#的寫法,使用object即可,但若用C++,51行為pointer寫法,55行為reference寫法,但不能使用object寫法。
See Also
(原創) 什麼是物件導向(Object Oriented)? (初級) (C/C++/C#)
(原創) 何時該使用object? 何時該使用reference? 何時該使用pointer? (初級) (C++) (OO C++)


Student(
浙公网安备 33010602011771号