精華區beta Programming 關於我們 聯絡資訊
※ 引述《LPH66.bbs@ptt.cc (涼宮春日症候群)》之銘言: > ※ 引述《quota@kkcity.com.tw (我要出清)》之銘言: > : CBox box1(78.0, 24.0, 18.0); > : CBox box2 = box1; > : 請問我的box2是不是就是box1這個物件呢? > : 謝謝 > 不是 > box2是複製box1的內容 所以是兩個物件 > 這和Java不一樣 > Java裡box2就是box1 是同一個物件 那現在我有一個問題... 我想驗證 這兩個是不同的物件 所以..剛剛看了一下程式碼 因為m_Length m_Breadth m_Height 都設定為私有成員 所以我無法直接傳值給他 而這三個成員又是在建構子內得到內容 也就是物件建立的時候.. 請教一下..如果我想改box2或box1的 m_Height為15 我該如何下手呢? 謝謝 附上程式碼 #include <iostream> using namespace std; class CBox { public: CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0): m_Length(lv), m_Breadth(bv), m_Height(hv) { cout << endl << "Constructor called."; } double Volume() { return m_Length*m_Breadth*m_Height; } private: double m_Length; double m_Breadth; double m_Height; friend double BoxSurface(CBox aBox); }; double BoxSurface(CBox aBox) { return 2.0*(aBox.m_Length*aBox.m_Breadth + aBox.m_Length*aBox.m_Height + aBox.m_Height*aBox.m_Breadth); } int main() { CBox box1(78.0, 24.0, 18.0); CBox box2 = box1; // Initialize box2 with box1 cout << endl << "box1 volume = " << box1.Volume() << endl << "box2 volume = " << box2.Volume(); cout << endl; system("PAUSE"); return 0; } -- ┌─────KKCITY─────┐ 人人可架站,經營社群聯誼天地 bbs.kkcity.com.tw 歡迎社團/班系/歌友/藝文創作/公益申請 └──From:218.169.114.97 ──┘ KKCity 開設自己喜愛的主題BBS --