美文网首页
静态变量

静态变量

作者: 当时光一去不复返时 | 来源:发表于2016-11-12 16:05 被阅读0次
#include<iostream>
using namespace std;
class Student
{
    static int num;    //静态变量
    int id;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
       
    
    int getid()
    {
        id=0;
        return num;
    }
    static int getNum()
    {
        return num;
    }
  
};
int Student::num=0;    //静态变量初始化
int main()
{
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"student num1"<<t2.getNum()<<endl;
    delete t3;
    cout<<"student num2"<<t3->getNum()<<endl;
}

相关文章

网友评论

      本文标题:静态变量

      本文链接:https://www.haomeiwen.com/subject/lddqpttx.html