請教c++的類別

看板Programming作者時間18年前 (2006/10/27 20:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
請教一下,為何下面兩個例子, 第一個例子的box2的值是25 而第二個例子的box2的值是500呢? 附上範例: 第一個例子 #include <iostream> using namespace std; class CBox { public: double m_Length; double m_Breadth; double m_Height; }; int main() { CBox box1; CBox box2; double boxVolume = 0.0; box1.m_Height = 20; box1.m_Length = 20; box1.m_Breadth =20; box2.m_Height = box1.m_Height - 10; box2.m_Length = box1.m_Length/2.0; box2.m_Breadth = 0.25*box1.m_Length; boxVolume = box1.m_Height*box1.m_Length*box1.m_Breadth; cout << endl << "Volume of box1 =" << boxVolume; cout << endl << "Volume of box2 =" << box2.m_Height+ box2.m_Length+ box2.m_Breadth; cout <<endl; system("PAUSE"); return 0; } 第二個例子 #include <iostream> using namespace std; class CBox { public: double m_Length; double m_Breadth; double m_Height; double Volume() { return m_Length*m_Breadth*m_Height; } }; int main() { CBox box1; CBox box2; double boxVolume = 0.0; box1.m_Height = 20; box1.m_Length = 20; box1.m_Breadth =20; box2.m_Height = box1.m_Height - 10; box2.m_Length = box1.m_Length/2.0; box2.m_Breadth = 0.25*box1.m_Length; boxVolume = box1.Volume(); cout << endl << "Volume of box1 = " << boxVolume; cout << endl << "Volume of box2 = " << box2.Volume(); cout << endl; system("PAUSE"); return 0; } 謝謝 -- ┌─────KKCITY─────┐ ◢ 想要成立班系社團站台嗎? bbs.kkcity.com.tw │ █ KKcity即日起開放BBS站申請囉! └──From:140.109.139.91 ──┘ ◥ ╲ 免程式技術、硬體成本的選擇!! --
文章代碼(AID): #15GVJ_00 (Programming)
討論串 (同標題文章)
以下文章回應了本文
完整討論串 (本文為第 1 之 2 篇):
文章代碼(AID): #15GVJ_00 (Programming)