#include<iostream>
#include <string>
using namespace std;
class User
{
private:
string name;
public:
User(string name)
{
this->name = name; //(1)
}
~User()
{
;
}
void setName(string name)
{
this->name = name;
}
string getName()
{
return name;
}
void sendMessage(string message);
};
class ChatRoom
{
public:
static void showMessage(User* user, string message)
{
cout << "{" <<user->getName() << "}:" << message << endl;
}
};
void User::sendMessage(string message)
{
ChatRoom::showMessage(this,message); //(2)
}
class ChatRoomSystem
{
public:
void startup()
{
User* zhang = new User("John");
User* li = new User("Leo");
zhang->sendMessage("Hi! Leo!");
li->sendMessage("Hi! John!");
}
void join(User* user)
{
user->sendMessage("Hello Everyone! I am " + user->getName()); //(3)
}
};
int main()
{
ChatRoomSystem* crs = new ChatRoomSystem(); //(4)
crs->startup();
crs->join(new User("Wayne")); //(5)
delete crs;
return 0;
}
答案:
(1) this->name
(2) ChatRoom::showMessage
(3) user->sendMessage
(4) new ChatRoomSystem()
(5) new User