#ifndef CLIENT_H
#define CLIENT_H
#include
using namespace std;
#include
class Client
{
private:
static string ServerName;
static int ClientNum;
static const int MAX = 10;
string ClientName[MAX];
public:
static void ChangeServerName();
void AddClient();
int Menu()const;
friend ostream & operator<<( ostream &, const Client & );
};
#endif
// 实现文件
#include "client.h"
string Client::ServerName = "Server";
int Client::ClientNum = 0;
void Client::ChangeServerName()
{
cin.ignore();
cout << "原来的服务器名称:"<< ServerName << endl;
cout << "输入新的服务器名称:";
getline( cin, ServerName );
cout << "现在的服务器名称:" << ServerName << endl;
}
void Client::AddClient()
{
cin.ignore();
cout<<"输入新增加的客户的名称:";
getline(cin,ClientName[ClientNum]);
++ClientNum;
}
int Client::Menu()const
{
int choice;
cout<<"1、添加客户 ";
cout<<"2、更改服务器名称 ";
cout<<"3、显示客户信息 ";
cout<<"4、退出 ";
cout<<"请选择所要执行的操作: ";
cin>>choice;
return(choice);
}
ostream & operator<< ( ostream & os, const Client & client )
{
os << "服务器名称:" << client.ServerName << endl;
os << "现有客户数量:" << client.ClientNum << endl;
os << "现有客户的名称: " ;
if ( client.ClientNum == 0 )
os << "sorry,目前没有客户端连接。" << endl;
else
{
os << endl;
for( int i = 0; i < client.ClientNum; i++ )
...{
os << "#" << i << ": " ;
os << client.ClientName[i] << endl;
}
}
return os;
}
//主函数
#include "client.h"
void main()
{
Client c;
while(1)
{
int choice=c.Menu();
switch(choice)
{
case 1: c.AddClient();
break;
case 2: c.ChangeServerName();
break;
case 3: cout << c;
break;
case 4: return;
}
}
}
|