題目:聲明字符型靜態數據成員ServerName,保存其服務器名稱;聲明整型靜態數據成員ClientNum,記錄已定義的客戶數量;定義靜態函數ChangeServerName()改變服務器名稱.在頭文件client.h中聲明類,在文件clent.cpp中實現,在文件test.cpp中測試這個類,觀察相應的成員
下面是我寫的程序:
#include
using namespace std;
class CLIENT
{
public:
CLIENT(char name,int num)
{
ServerName=name;
ClientNum=num;
}
CLIENT(CLIENT &p);
~CLIENT() {}
void showServerName() {}
void showClientNum() {}
void showChangeServerName();
private:
static char ServerName;
static int ClientNum;
};
#include"client.h"
CLIENT::ServerName(char name)
{
ServerName=name;
}
CLIENT::ClientNum(int num)
{
ClientNum=num;
}
char ServerName='I';
int ClientNum=0;
#include
#include"text.h"
int main()
{
CLIENT n(7,'W');
cout<<"已定義客戶數量:"<<n.showClientNum()<<endl;
n.showClientNum();
cout<<"服務器名稱改為:"<<n.showChangeServerName()<<endl;
n.showChangeServerName();
}
在頭文件client.h中聲明類:
class CLIENT
{
public:
CLIENT(char name,int num);
~CLIENT();
char getServerName() ;
int getClientNum();
static void ChangeServerName(char newName);
private:
static char ServerName;
static int ClientNum;
};
在文件clent.cpp中實現:
CLIENT::CLIENT(char name,int num)
{
ServerName=name;
ClientNum=num;
}
CLIENT::~CLIENT() {}
char CLIENT::getServerName()
{
return ServerName;
}
int CLIENT::getClientNum()
{
return ClientNum;
}
void CLIENT::ChangeServerName(char newName)
{
ServerName = newName;
}
在文件test.cpp中測試這個類,觀察相應的成員:
#include
using namespace std;
int main6()
{
CLIENT n(7,'W');
cout<<"qqqqq:"<<n.getServerName()<<endl;
cout<<"aaaaa:"<<n.getClientNum()<<endl;
n.ChangeServerName('X');
cout<<"qqqqq:"<<n.getServerName()<<endl;
cout<<"aaaaa:"<<n.getClientNum()<<endl;
}