include
include
using namespace std;
class Human {
public:
Human(int age,int number,string addr);
~Human();
static void getCount();
private:
static int count;//需要在外部定义
int age;
int number;
string* addr;
};
int Human::count = 0;//在此处定义
Human::Human(int age, int number,string addr) {
this->age = age;
this->number = number;
this->addr = new string(addr);
this->count++;
cout << age << " " << number << " " << *(this->addr) << endl;
cout << "构造函数" << endl;
}
Human::~Human() {
delete addr;
cout << "析构函数" << endl;
}
void Human::getCount() {----------------------------------------------------》重要
//因为无法知道是哪个具体的对象---》(static大众情人)
//所以无法用this,和类中的气体成员
//除非有参数的的静态成员函数,这样可以传参进入获得,自己的变量
cout << count << endl;
}
int main() {
Human h1(1,2,"A");
Human h2(5, 4, "B");
h1.getCount();
h2.getCount();
Human::getCount();//也可以这样调用的
}