c++静态变量和静态函数

#include <iostream>
using namespace std;
class Test
{
private:
  static int x; //静态变量
  int n;
public:
  Test(){};
  Test(int a,int b)
  {
    x=a;
    n=b;
  }
  static int func()
  {
    return x;
  }
  static void sfunc(Test &r,int a)
  {
    r.n=a;
  }
  int Getn()
  {
    return n;
  }
};

int Test::x=25; //静态变量初始化在类之外
int main()
{
  cout<<Test::func();
  Test b,c;
  b.sfunc(b,58);
  cout<<" "<<b.Getn();
  cout<<" "<<b.func();
  cout<<" "<<c.func();
  Test a(24,56);
  cout<<" "<<a.func()<<" "<<b.func()<<" "<<b.func()<<endl;
  return 1;
}

 

posted @ 2020-12-13 11:44  bobo哥  阅读(142)  评论(0编辑  收藏  举报