#include<iostream>
using namespace std;
class Box
{
public:
Box();
Box(double lengthvalue,double breadthvalue,double heightvalue);
double volume()const;
int comparevolume(const Box& otherbox)const;
int getobjectcount()const {return objectcount;}
private:
static int objectcount;
double length;
double breadth;
double height;
};
Box::Box()
{
cout<<endl<<"Default constructor called"<<endl;
++objectcount;
length=breadth=height=1;
}
Box::Box(double lengthvalue,double breadthvalue,double heightvalue):length(lengthvalue),
breadth(breadthvalue),height(heightvalue)
{
cout<<endl
<<"constructor called"
<<endl;
++objectcount;
if(length<=0)
length=1.0;
if(breadth<=1.0)
breadth=1.0;
if(height<=0)
height=1.0;
}
double Box::volume()const
{
return length*breadth*height;
}
int Box::comparevolume(const Box& otherbox)const
{
if(this->volume()<otherbox.volume())
return 1;
else
return 0;
}
int Box::objectcount=0;
int main()
{
cout<<endl;
Box firstbox(17.0,11.0,5.0);
cout<<"object count is:"<<firstbox.getobjectcount()<<endl;
Box boxes[5];
cout<<"objects count is:"<<firstbox.getobjectcount()<<endl;
cout<<"volume of the first box="<<firstbox.volume()<<endl;
const int count=sizeof boxes/sizeof boxes[0];
cout<<"The boxes array has "<<count<<"elements."<<endl;
cout<<"Each element occupies"<<sizeof boxes[0]<<"bytes."
<<endl;
for(int i=0;i<count;i++)
cout<<"volume of boxes["<<i<<"]="<<boxes[i].volume()
<<endl;
return 0;
}
正文
C++(类的静态成员运用)2005-07-15 12:07:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/jay0518/2841.html
阅读(3984) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论