求解C加加
定义一个DOG类,它用静态数据成员DOGS记录了DOG的数目,静态成员函数CATDOGS用来存取DOGS.设计并测试这个类...
定义一个DOG类,它用静态数据成员DOGS记录了DOG的数目,静态成员函数CATDOGS用来存取DOGS.设计并测试这个类
展开
1个回答
展开全部
#include<iostream.h>
//DOG类
class DOG;
class DOG
{
public:
DOG()
{
++DOGS;
}
virtual~DOG()
{
--DOGS;
}
public:
static unsigned int GetDOGS()
{
return DOGS;
}
private:
static unsigned int DOGS;
};
unsigned int DOG:: DOGS=0;
//测试
void main()
{
cout<<DOG::GetDOGS()<<endl;
DOG* p[5]={
new DOG(),
new DOG(),
new DOG(),
new DOG(),
new DOG()
};
cout<<DOG::GetDOGS()<<endl;
delete p[4];
delete p[3];
cout<<DOG::GetDOGS()<<endl;
p[3]=new DOG(); cout<<DOG::GetDOGS()<<endl;
for(int i=0;i<4;i++) delete p[i];
cout<<DOG::GetDOGS()<<endl;
}
//DOG类
class DOG;
class DOG
{
public:
DOG()
{
++DOGS;
}
virtual~DOG()
{
--DOGS;
}
public:
static unsigned int GetDOGS()
{
return DOGS;
}
private:
static unsigned int DOGS;
};
unsigned int DOG:: DOGS=0;
//测试
void main()
{
cout<<DOG::GetDOGS()<<endl;
DOG* p[5]={
new DOG(),
new DOG(),
new DOG(),
new DOG(),
new DOG()
};
cout<<DOG::GetDOGS()<<endl;
delete p[4];
delete p[3];
cout<<DOG::GetDOGS()<<endl;
p[3]=new DOG(); cout<<DOG::GetDOGS()<<endl;
for(int i=0;i<4;i++) delete p[i];
cout<<DOG::GetDOGS()<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询