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;
}
相关推荐
geovindu1 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu3 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
derek19973 小时前
软考软件设计师:主键、传递依赖和冗余,三问其实是一件事
c语言·c++·计算机网络·软件工程·学习方法
凌冰_4 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏4 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
艾莉丝努力练剑4 小时前
【AI大模型接入SDK】解决Unicode码转化以及界面变形问题
前端·c++·人工智能·学习·安全·架构
djjjx.4 小时前
Mysql数据库基础篇(十一):索引的常见操作、聚簇与非聚簇索引、为什么其他数据结构不能作为索引的底层结构
数据结构
ShineWinsu5 小时前
对于Qt:事件分发器以及事件过滤器的解析
c++·qt·面试·gui·客户端·事件过滤器·事件分发器
饮水桶_T6 小时前
C++ STL00 | 六大核心组件协作框架 笔记
开发语言·算法·百度·facebook·新浪微博
悠仁さん7 小时前
【C++】unordered_map_set 模拟实现
开发语言·c++