類的轉換函式
轉換函式的特點:
(1)必須是類方法。
(2)沒有返回值。
(3)沒有引數。
(4)只能轉換為內建型別,不可以轉換為自定義型別。
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include <iostream> using std::cout;
using std::endl;
class Stonewt
{ private :
enum {Lbs_per_stn = 10}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
void Show() const {
cout << " this = " << this << ", (stone, pds_left) = (" << stone << ", " << pds_left << "), pounds = " << pounds << endl;
}
public :
//explicit Stonewt(double lbs); // constructor for double pounds
Stonewt( double lbs); // constructor for double pounds
Stonewt( int stn, double lbs); // constructor for stone, lbs
Stonewt(); // default constructor
Stonewt( const Stonewt &st); // copy constructor
~Stonewt();
Stonewt& operator=( const Stonewt &st); // copy constructor
operator double () const ;
}; // construct Stonewt object from double value Stonewt::Stonewt( double lbs)
{ cout << "enter " << __func__ << "(double lbs) ;
stone = int (lbs) / Lbs_per_stn; // integer division
pds_left = int (lbs) % Lbs_per_stn + lbs - int (lbs);
pounds = lbs;
Show();
cout << "leave " << __func__ << "(double lbs) ;
} // construct Stonewt object from stone, double values Stonewt::Stonewt( int stn, double lbs)
{ cout << "enter " << __func__ << "(int stn, double lbs) ;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
Show();
cout << "leave " << __func__ << "(int stn, double lbs) ;
} Stonewt::Stonewt() // default constructor, wt = 0
{ cout << "enter " << __func__ << "() ;
stone = pounds = pds_left = 0;
Show();
cout << "leave " << __func__ << "() ;
} Stonewt::Stonewt( const Stonewt &st) // copy constructor
{ cout << "enter " << __func__ << "(const &) ;
stone = st.stone;
pounds = st.pounds;
pds_left = st.pds_left;
Show();
cout << "leave " << __func__ << "(const &) ;
} Stonewt::~Stonewt() // destructor
{ cout << "enter " << __func__ << "() ;
Show();
cout << "leave " << __func__ << "() ;
} Stonewt& Stonewt::operator=( const Stonewt &st)
{ cout << "enter " << __func__ << "(const &) ;
if ( this == &st){
cout << "same object ;
return * this ;
}
stone = st.stone;
pounds = st.pounds;
pds_left = st.pds_left;
Show();
cout << "leave " << __func__ << "(const &) ;
return * this ;
} // conversion functions Stonewt::operator double () const
{ cout << "enter " << __func__ << "() ;
Show();
cout << "leave " << __func__ << "() ;
return pounds;
} int main(){
Stonewt obj1 = 275; // uses constructor to initialize
obj1 = 276.8; // uses constructor for conversion
cout << double (obj1) << endl;
} |
測試結果:
enter Stonewt(double lbs)
this = 0x7fff20137160, (stone, pds_left) = (27, 5), pounds = 275
leave Stonewt(double lbs)
enter Stonewt(double lbs)
this = 0x7fff20137180, (stone, pds_left) = (27, 6.8), pounds = 276.8
leave Stonewt(double lbs)
enter operator=(const &)
this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8
leave operator=(const &)
enter ~Stonewt()
this = 0x7fff20137180, (stone, pds_left) = (27, 6.8), pounds = 276.8
leave ~Stonewt()
enter operator double()
this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8
leave operator double()
276.8
enter ~Stonewt()
this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8
leave ~Stonewt()
相關文章
- 轉換函式函式
- 函式式 Java 到函式式 Kotlin 的轉換函式JavaKotlin
- 函式組:TRUX 包含很多的轉換函式函式UX
- numtoyminterval函式——數字轉換函式函式
- php轉換ip函式PHP函式
- js日期轉換函式JS函式
- 轉換時間戳的函式時間戳函式
- 日期轉換為raw的函式函式
- oracle 10g函式大全--轉換函式Oracle 10g函式
- Oracle OCP(05):轉換函式Oracle函式
- 8.轉換文字函式函式
- 混沌 IN C++::轉換函式C++函式
- 索引ROWID轉換函式索引函式
- 轉:類似SQL中的split函式SQL函式
- ORACLE單行函式與多行函式之五:轉換函式示例Oracle函式
- 將數值轉換為字串的函式字串函式
- oracle 全形半形轉換函式Oracle函式
- c++ operator typename 轉換函式C++函式
- string大小寫轉換函式函式
- SQL 數字轉換英文函式SQL函式
- 各類檔案管理函式 (轉)函式
- 小談漢字轉換成拼音輔助函式和輔助類函式
- 建構函式定義的隱式型別轉換函式型別
- 呼叫類庫函式的簡單加密(轉的)函式加密
- mysql和oracle字串編碼轉換函式,字串轉位元組函式例子MySqlOracle字串編碼函式
- scala中隱式轉換之隱式轉換呼叫類中本不存在的方法
- oracle內部轉換函式雜談Oracle函式
- GBK中文繁簡轉換函式函式
- MySQL字串函式 字串大小寫轉換MySql字串函式
- Base64與BLOB 轉換函式函式
- 浮點數轉換成字串函式字串函式
- oracle時間間隔轉換函式Oracle函式
- oracle中進位制轉換函式Oracle函式
- [轉]decode函式和行列互換函式
- to_char函式格式轉換參考函式
- 函式組:STXK(SAPscript 文字轉換)函式
- C#全形半形轉換函式C#函式
- GaussDB: db2->gaussdb 函式轉換DB2函式