Re: [問題] 類別建構子問題
int i;
這樣的動作你應該不陌生,
宣告一個變數名稱為i,型別是int
test t;
這也是類似的動作,
宣告一個變數名稱為t,型別是test
但C++有一個機制,叫做建構子
在變數宣告的時候,就會自動呼叫與類別同名的函式
這樣就可以做一些初始化的動作
http://nopaste.csie.org/0f0fc
以這個程式為例,當我們寫 test t; 時
它會自動幫我們做一件事:呼叫 t.test();
http://nopaste.csie.org/747a8
再以這個程式為例,當我們寫 test t(100); 時
就是這樣的兩個步驟:
test t;
t.test(100);
最後以你的程式 http://nopaste.csie.org/e3877 來說
Cubic first(n1),second(n2),third(0);
就類似這個樣子:
Cubic first, second, third; // 跟 int i, j, k; 的意思差不多
first.Cubic(n1);
second.Cubic(n2);
third.Cubic(0);
順便說明這行:third = first.cub_sum(second);
假如你的n1=8, n2=6,那first裡的a是8*8*8=512,second裡的a是6*6*6=216
Cubic cub_sum(Cubic b) // 此時的 b 是 second,a=216
{
this->a = this->a + b.a;
return *this;
}
--
如果你還有問題的話,就請繼續po板討論吧,我不喜歡用水球聊程式...XD
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.117.171.46
→
02/19 13:10, , 1F
02/19 13:10, 1F
→
02/19 18:44, , 2F
02/19 18:44, 2F
→
02/19 18:46, , 3F
02/19 18:46, 3F
→
02/19 22:16, , 4F
02/19 22:16, 4F
討論串 (同標題文章)