c++(编写一个含有静态成员(静态数据成员、静态成员函数)的狗类(dog))

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;
}


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值