c++day5

1:写一个Msg消息队列类,要求有以下功能 Msg m() 构造函数,创建消息队列 m[1] << "hello" 向消息队列的1频道写入数据"hello",当然可以是其他的字符串数据 m[2] >> 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() ;// 终端输出 "黄色"(未运行检查)

相关推荐
崎岖Qiu4 分钟前
【JVM篇11】:分代回收与GC回收范围的分类详解
java·jvm·后端·面试
27669582922 小时前
东方航空 m端 wasm req res分析
java·python·node·wasm·东方航空·东航·东方航空m端
许苑向上2 小时前
Spring Boot 自动装配底层源码实现详解
java·spring boot·后端
喵叔哟2 小时前
31.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--单体转微服务--财务服务--收支分类
java·微服务·.net
再学一点就睡2 小时前
手写 Promise 静态方法:从原理到实现
前端·javascript·面试
codu4u13143 小时前
Maven中的bom和父依赖
java·linux·maven
再学一点就睡3 小时前
前端必会:Promise 全解析,从原理到实战
前端·javascript·面试
呦呦鹿鸣Rzh3 小时前
微服务快速入门
java·微服务·架构
今天也好累4 小时前
C 语言基础第16天:指针补充
java·c语言·数据结构·笔记·学习·算法
没有bug.的程序员5 小时前
《Spring Security源码深度剖析:Filter链与权限控制模型》
java·后端·spring·security·filter·权限控制