静态变量

2016-11-12  本文已影响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;
}
上一篇 下一篇

猜你喜欢

热点阅读