韶关市网站建设_网站建设公司_响应式网站_seo优化
2026/1/5 19:28:34 网站建设 项目流程

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();//也可以这样调用的

}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询