|
#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
// 實現(xiàn)文件
#include "client.h"
string Client::ServerName = "Server";
int Client::ClientNum = 0;
void Client::ChangeServerName()
{
cin.ignore();
cout << "原來的服務器名稱:"<< ServerName << endl;
cout << "輸入新的服務器名稱:";
getline( cin, ServerName );
cout << "現(xiàn)在的服務器名稱:" << 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<<"請選擇所要執(zhí)行的操作: ";
cin>>choice;
return(choice);
}
ostream & operator<< ( ostream & os, const Client & client )
{
os << "服務器名稱:" << client.ServerName << endl;
os << "現(xiàn)有客戶數(shù)量:" << client.ClientNum << endl;
os << "現(xiàn)有客戶的名稱: " ;
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;
}
//主函數(shù)
#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;
}
}
}
|