請教c++的類別
請教一下,為何下面兩個例子,
第一個例子的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 》──┘ ◥ ╲ 免程式技術、硬體成本的選擇!!
--
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
Programming 近期熱門文章
PTT數位生活區 即時熱門文章