【问题描述】声明字符型静态数据成员ServerName,保存服务器名称;整型静态数据成员ClientName记录已定义的客户数量;定义静态成员函数changeServerName()改变服务器名称。在头文件client.h中声明类,在文件client.cpp中实现,在文件lab.cpp中测试这个类,观察相应的成员变量取值的变化情况。
【输入形式】输入客户数量和每个客户的名称
【输出形式】输出每个客户名称及当前客户数量
【样例输入1】
1
a
【样例输出1】
a
1
【样例输入2】
3
a b c
【样例输出2】
a
1
b
2
c
3
【样例说明】
【评分标准】
主程序lab.cpp
#include <iostream>
#include <vector>
#include "client.h"
using namespace std;
int main() {
vector<Client> clients;
int num_clients;
cin >> num_clients;
for (int i = 0; i < num_clients; ++i) {
string name;
cin >> name;
clients.emplace_back(name);
}
int i = 1;
for (const auto& client : clients) {
cout << client.getName() << endl << i++ << endl;
}
Client::changeServerName("New Server Name");
return 0;
}
client类cpp文件:
#include "client.h"
string Client::ServerName = "";
int Client::ClientName = 0;
void Client::changeServerName(const string& new_name) {
ServerName = new_name;
}
Client::Client(const string& name) : m_Name(name) {
++ClientName;
}
string Client::getName() const {
return m_Name;
}
client头文件h
#pragma once
#include <string>
using namespace std;
class Client {
public:
static string ServerName;
static int ClientName;
static void changeServerName(const string& new_name);
Client(const string& name);
string getName() const;
private:
string m_Name;
};