【静态成员】

【问题描述】声明字符型静态数据成员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; 
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武帝为此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值