c++day5

1:写一个Msg消息队列类,要求有以下功能 Msg m() 构造函数,创建消息队列 m1 << "hello" 向消息队列的1频道写入数据"hello",当然可以是其他的字符串数据 m2 >> str :从2频道读取消息,并且将读到的消息存入str中 析构函数:删除消息队列

复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
 
using namespace std;
 
class Msg{
private:
    string m[2];
public:
    Msg(){
        cout << "消息队列创建成功" << endl;
    }
    ~Msg(){
        cout<< "成功删除消息队列" << endl;
    }
    string& operator[](int channel){
        return m[channel];
    }
    friend void operator>>(string& channel,const string& s);
    friend string& operator<<(string& channel,string& s);
};
 
void operator>>(string& channel,const string& s){
    channel=s;
    cout << "成功将消息: " << s << "  写入消息队列中" << endl;
}
 
string& operator<<(string& channel,string& s){
    s=channel;
    cout << "成功读取到消息" << endl;
    return s;
}
 
int main(int argc,const char** argv){
    Msg m;
    m[0]>>"你好";
    string str;
    m[1]<< str;
    cout << str<<endl;
	return 0;
}

2:写一个员工类 Employee,有一个多态函数叫做 getSalary 有一个 Cleanner 保洁类,继承自员工类:每个月获得 5000 工资 有一个 Coder 程序员类,继承自员工类,每个月获得 10000工资 有一个 Manger 经理类,继承自员工类,每个月获得 15000工资 写一个发工资的函数,要求,能够为所有员工发放工资,即使追加新的岗位,也不会改变这个函数的逻辑

复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Employee{
private:

public:
    virtual void getSalary()
    {
        cout<<"salary"<<endl;
    }

};
class Cleanner:public Employee{
    void getSalary()
    {
        cout<<"5000"<<endl;
    }
};
class Coder:public Employee{
    void getSalary()
    {
        cout<<"10000"<<endl;
    }
};
class Manger:public Employee{
    void getSalary()
    {
        cout<<"15000"<<endl;
    }
};
void outsalary(Employee& G)
{
    G.getSalary();
}


int main(int argc,const char** argv){
    Cleanner C;
    Manger M;
    Coder B;
    outsalary(C);

return 0;
}                                         

3:写一个基类叫做颜色 写3个派生类,红绿蓝以及需要存在的其他颜色,继承自颜色类 要求实现以下功能: Red r Green g; Color new_color = r + g new_color.show() ;// 终端输出 "黄色"(未运行检查)

相关推荐
Wang's Blog1 分钟前
Java 项目实战: 交友项目 - 阿里云概述与项目中的云服务选型
java·阿里云·交友
2601_9620710021 分钟前
Kimi K3 vs DeepSeek V4-Pro实测对比:2026年同一入口十道题,耗时与token消耗差在哪
android·java·数据库
多弗朗皮卡丘25 分钟前
C++异常
开发语言·c++·异常
米糕闯编程30 分钟前
鱼香ros2(十七)C++小海龟巡逻
开发语言·c++·机器人·ros2·服务·小海龟
用户37215742613533 分钟前
如何使用 Java 提取 PPT 中的文字:整份文档、指定幻灯片与表格内容
java
ManageEngineITSM1 小时前
工单优先级怎么定?影响×紧急度矩阵的设计与落地方法
java·大数据·运维·前端·数据库·变更管理
\光辉岁月/1 小时前
15.JavaSE-网络
java·开发语言·网络
const_qiu1 小时前
第二章 2.4:搭建 Monorepo 骨架 + 共享包 @autix/contracts
java·人工智能·arcgis
救救孩子把1 小时前
AI面试八股文-03_3-RAG 系统面试题
人工智能·redis·面试·agent·rag
栈帧之外1 小时前
【C++】红黑树实现
开发语言·c++