试题六(共 15 分)阅读下列说明和 C++代码,填补代码中的空缺,将解答填入答

admin2022-08-02  31

问题 试题六(共 15 分)阅读下列说明和 C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。【说明】以下 C++代码实现一个简单的聊天室系统(ChatRoomSystem),多个用户 (User)可以向聊天室(ChatRoom)发送消息,聊天室将消息展示给所有用户。 类图如图 6-1 所表示。【C++代码】#include<iostream>#include <string> using namespace std; class User {private:string name; public:User(string name){    (1) =name;}~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) {  (2) (this,message);}class ChatRoomSystem{public: . .void startup0(){User* zhang = new User(“John"); User* li = new User("Leo"); zhang->sendMessage("Hi! Leo!");li_>sendMessage("Hi! John!"); }void join(User* user) {    (3)   ("HeIIoEveryone!l am"+user->getName()); . ;} . };int main(){ChatRoomSystem*crs=  (4) ; crs->startup();crs->join(  (5) ("Wayne")); delete crs; }/* 程序运行结果: [John]:Hi! Leol [Leo]:Hi! John![Wayne】:Hello Everyone!Iam Wayne/*

选项

答案

解析 1、this->name
2、ChatRoom::showMessage
3、user->sendMessage
4、new ChatRoomSystem()
5、new User
转载请注明原文地址:https://tihaiku.com/congyezige/2425940.html

最新回复(0)