c++ 派生类

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <sys/ipc.h>
#include <sys/msg.h>

using namespace std;

struct xiaoxi{
    long type;
    char buf[1024];
};

class Msg{
private:
    key_t key;
    int msg_id;
    struct xiaoxi text;
public:
    Msg(){
        key = ftok(".",200);
        msg_id = msgget(key,IPC_CREAT | 0664);
        //text.buf = new char[1024];
    }
    
    Msg& operator[](const long& type){
        text.type = type;
        return *this;
    }

    Msg& operator<<(const char* r){
        strcpy(text.buf,r);
        msgsnd(msg_id,&text,sizeof(text.buf)+1,0);
        return *this;
    }

    Msg& operator>>(char* r){
        msgrcv(msg_id,&text,sizeof(text.buf),text.type,0);
        strcpy(r,text.buf);
        return *this;
    }

    ~Msg(){
        msgctl(msg_id,IPC_RMID,NULL);
        //delete [] text.buf;
    }
};


int main(int argc,const char** argv)
{
    Msg m;
    char str[1024];
    m[1] << "你好";
    m[2] >> str;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Employee{
private:
    int price = 0;
public:
    void addPrice(int salary = 0){
        price += salary;
    }

    virtual void getsalaries() = 0;

    void showsalaries(){
        cout << "发放薪水:" << price << endl;
    }
};

class Cleanner:public Employee{
private:

public:
    void getsalaries(){
        addPrice(5000);
    }
};

class Coder:public Employee{
private:

public:
    void getsalaries(){
        addPrice(10000);
    }
};

class Manger:public Employee{
private:

public:
    void getsalaries(){
        addPrice(15000);
    }
};

class Boos{
private:
    Employee* list[100];
    int len = 0;
public:
    void add_list(Employee* worker){
        list[len] = worker;
        len ++;
    }

    Boos& operator<<(Employee& r){
        add_list(&r);
        return *this;
    }

    void pay_salaries(){
        for(int i=0;i<len;i++){
          list[i]->getsalaries();
        }

        for(int i=0;i<len;i++){
        list[i]->showsalaries();
        }
    }
};

int main(int argc,const char** argv)
{
    Cleanner qj;
    Coder dm;
    Manger jl;
    Coder dm2;
    Boos lb;

    lb << qj << dm << jl << dm2;

    lb.pay_salaries();
	return 0;
}
cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Color{
private:

public:
	virtual void show(){
	}
};

class Red:public Color{
private:

public:
	void show(){
		cout << "红色" << endl;
	}
};

class Green:public Color{
private:

public:
	void show(){
		cout << "绿色" << endl;
	}
};

class Blue:public Color{
private:

public:
	void show(){
		cout << "蓝色" << endl;
	} 
};

Color* operator+(Color& r,Color& l){
	return new Color;
}

void shuchu_color(Color** queue){
	for(int i=0;i<3;i++){
		queue[i]->show();
	}
}

int main(int argc,const char** argv)
{
	Color color;
	Red red;
	Green green;
	Blue blue;

	Color* queue[7] = {&red,&green,&blue};
	
	shuchu_color(queue);
	return 0;
}
相关推荐
m0_7202450110 小时前
1543.统计好三元组(简单)
开发语言·算法
To_OC10 小时前
LC 560 和为 K 的子数组:前缀和配哈希表,这对组合我是真的服了
javascript·算法·程序员
hans汉斯11 小时前
《软件工程与应用》期刊推荐&10月版面征稿中
图像处理·人工智能·深度学习·算法·音视频·软件工程
叠层归一研究院11 小时前
AGI 系统(八):符号范畴嵌入函子 — SymCat ↪ C_M107 严格化
c语言·开发语言·人工智能·算法·transformer·agi
其美杰布-富贵-李12 小时前
08 进阶评估指标与算法特定指标
人工智能·算法
.道阻且长.13 小时前
5.LeetCode算法习题讲解--双指针--有效三角形的个数
算法·leetcode·职场和发展
我是海飞13 小时前
杰理JL703N SSD1306 OLED(128x64) 点阵屏移植说明
单片机·算法·嵌入式·音频·杰理
wabs66614 小时前
关于图论【最短路径之Bellman_ford 算法(单源有限最短路)|卡码网96.城市间货物运输III的思考】
数据结构·算法·图论·卡码网·bellman_ford·单源有限最短路
wuyk55515 小时前
2.队列:先进先出的线性数据结构
c语言·数据结构·stm32·单片机
会周易的程序员15 小时前
aiDgePLC — IEC61131-3 ST PLC 虚拟机调试与运行环境
c++·物联网·虚拟机·st·工业协议·iec61131·梯形图