wuyu_1996 2015-05-04 15:16 采纳率: 0%
浏览 2887

该类有个私有静态变量count记录该类的所有对象数

给定如下程序代码,请根据main函数中对该类的操作,补充类实现部分完成代码。

该类有个私有静态变量count记录该类的所有对象数,主函数将会在不同语句之后输出对象数,只有正确地实现该类,保证count正确记录该类的对象数,才能输出正确的结果。
#include

#include

using namespace std;

class Student {

private:

int id;

static int count;

public:

// 此处填写自己的代码

};

int Student::count;

void PrintCount() {

cout << "Total " << Student::count << " students" << endl ;

}

void Print(Student s) {

cout << "the id is " << s.id << endl ;

}

int main() {

Student::InitCount();

Student s;

PrintCount();

Student s1(10);

Student s2(s1);

PrintCount();

Print(s2); // 调用拷贝构造函数,调用结束调用析构函数

PrintCount();

return 0;

}

  • 写回答

2条回答 默认 最新

  • 知常曰明 2015-05-04 21:46
    关注
        // 此处填写自己的代码
        static void InitCount(){ count = 0; }
        Student()
        {
            Student::count++;
        }
        Student(int _id)
        {
            this->id = _id;
            Student::count++;
        }
        Student(Student *s0)
        {
            this->id = s0->id;
            Student::count++;
        }
        ~Student()
        {
            Student::count--;
        }
    

    有一点问题,最上面的provate:要改为public:

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿