1.编写一个含有静态成员(静态数据成员、静态成员函数)的狗类(dog)
具体要求如下:
(1)具有两个非静态数据成员,分别为age(年龄)、weight(体重)
(2)一个静态数据成员count用于记录实例化对象的个数
(3)要求类定义中有两个构造函数(有参数、无参数)形成构造函数重载,有复制构造函数,有析构函数
(4)有setage、setweight、showage、showweight4个成员函数用来设置和显示年龄和体重。
(5)有静态数据成员函数staticgetcount函数用于输出当前对象个数
//(6)完成并测试这个类并建立指向类对象的指针,通过指针访问对象成员。
```cpp
#include<iostream>
#include<cstdlib>
using namespace std;
class dog
{
public:
dog(int age,int weight);
dog() {};
dog(const dog &p);
~dog();
void setage(int age);
void setweight(int weight);
void showage();
void showweight();
static int staticgetcount();
private:
int age;
int weight;
static int count;
};
int dog::count=0;
dog::dog(int age,int weight)
{
this->age=age;
this->weight=weight;
count++;
}
dog::dog(const dog &p)
{
age=p.age;
weight=p.weight;
count++;
}
void dog::showage()
{
cout<<"The age is : "<<age<<endl;
}
void dog::showweight()
{
cout<<"THe weight is : "<<weight<<endl;
}
int dog::staticgetcount()
{
return count;
}
dog::~dog()
{
}
//main函数过于简单,需要的自添
int main()
{
dog A(10,100);
A.showage();
dog B (90,56);
B.showweight();
cout<<"The count is : ";
cout<<dog::staticgetcount()<<endl;
}