看板Programming
标 题请教c++的类别
发信站KKCITY (Fri Oct 27 19:46:22 2006)
转信站ptt!ctu-reader!ctu-peer!news.nctu!netnews.chu!Leo.mi.chu!zoonews.ee.nt
请教一下,为何下面两个例子,
第一个例子的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
》──┘ ◥ ╲ 免程式技术、硬体成本的选择!!
--