分享

实现客户机(Client)类

 古罗 2007-11-16
#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; } } }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多