陣列與物件
其實原文的boxes[5]是空的,我想這既然是陣列,那我就給值
這動作是對的還是錯的呢?
該行程式碼如下:
CBox boxes[5]={21,23,34,345,34};
但是讓我困惑的是..
boxes[3].value這是怎麼來的?
為何是345?
boxes執行的建構子是有參數的
那他是丟什麼值給lv、bv與hv呢?
懇請協助,謝謝
附上語法
#include <iostream>
using namespace std;
class CBox
{
public:
CBox(double lv, double bv = 1.0, double hv = 1.0): m_Length(lv),
m_Breadth(bv),
m_Height(hv)
{
cout << endl << "Constructor called.";
}
CBox()
{
cout << endl
<< "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
}
double Volume() const
{
return m_Length*m_Breadth*m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
int main()
{
CBox boxes[5]={21,23,34,345,34};
CBox cigar(8.0, 5.0, 1.0);
cout << endl
<< "Volume of boxes[3] = " << boxes[3].Volume()
<< endl
<< "Volume of cigar = " << cigar.Volume();
cout << endl;
system("PAUSE");
return 0;
}
--
┌─────◆KKCITY◆─────┐ ◢ ◤ 找歌最方便 KKBOX 歌詞搜尋!!
│ bbs.kkcity.com.tw │ \^_^ / ★http://www.kkbox.com.tw★
└──《From:218.169.114.97 》──┘ ◤ 唱片公司授權,音樂盡情下載
--
Programming 近期熱門文章
PTT數位生活區 即時熱門文章