【无标题】

发布于:2025-02-25 ⋅ 阅读:(76) ⋅ 点赞:(0)

2.

#include <iostream>

using namespace std;

class AB
{
	private:
		int a;
		int b;
	public:
		AB(int a=0,int b=0):a(a),b(b){}
		void setA(int l){a = l;}
		void setB(int l){b = l;}
		int getA(){return a;}
		int getB(){return b;}
		void show()
		{
			cout<<a <<" "<<b<<endl;
		}
};

class AA:public AB
{
	public:
		AA(int ab=0):AB(ab,ab){}
		void setA(int l)
		{
			AB::setA(l);
			AB::setB(l);
		}
		void setB(int l)
		{
			AB::setA(l);
			AB::setB(l);
		}
		void show()
		{
			AB::show();
		}
};

int main()
{
	AA q;
	q.setB(10);
	q.setA(11);
	q.show();
}

#include <iostream>

using namespace std;

class ABC 
{
	private:
		int a;
		int b;
		int c;
	public:
		ABC(int a=0,int b=0,int c=0)
			:a(a),b(b),c(c)
		{}
		void setA(int l){a = l;}
		void setB(int l){b = l;}
		void setC(int l){c = l;}
		int getA(){return a;}
		int getB(){return b;}
		int getC(){return c;}
		void show()
		{
			cout<<a<<" "<<b<<" "<<c<<endl;
		}
};

class AAC:public ABC
{
	public:
		AAC(int ab=0,int c=0)
			:ABC(ab,ab,c)
			{};
		void setA(int l)
		{
			ABC::setA(l);
			ABC::setB(l);
		}
		void setB(int l)
		{
			setA(l);
		}
		void setC(int l)
		{
			ABC::setC(l);
		}
		int getA(){return ABC::getA();}
		int getB(){return ABC::getB();}
		int getC(){return ABC::getC();}
		void show()
		{
			ABC::show();
		}
};

class AAA:public AAC
{
	public:
		AAA(int aaa = 0)
			:AAC(aaa,aaa)
		{}
		void setA(int l)
		{
			AAC::setA(l);
			AAC::setC(l);
		}
		void setB(int l)
		{
			setA(l);
		}
		void setC(int l)
		{
			setA(l);
		}
		int getA(){return AAC::getA();}
		int getB(){return AAC::getB();}
		int getC(){return AAC::getC();}
		void show()
		{
			AAC::show();
		}

};

int main()
{
	AAA z;
	z.setB(10);
	z.show();
}

3.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>


using namespace std;

class Msg{
private:
    key_t key;
    int id;
    int channel;
    struct msgbuf{
        long channel;
        char text[512];
    };
public:
    Msg(const string& filename = ""){
        key = ftok(filename.data(),1);
        id = msgget(key,IPC_CREAT | 0666);
    }

    ~Msg(){
        msgctl(id,IPC_RMID,0);
    }
    void send(const string& str){
        msgbuf buf = {0};    
        strcpy(buf.text,str.data());
        buf.channel = channel;
        msgsnd(id,&buf,str.length(),0);
    }
    string recv(int size=512){
        msgbuf buf = {0};
        msgrcv(id,&buf,size,channel,0);
        string str = buf.text;
        return str;
    }
    Msg& operator<<(const string& str)
    {
        send(str);
        return *this;
    }
    Msg& operator>>(string& str)
    {
        str = recv();
        return *this;
    }
    Msg& operator[](int channel);
};

Msg& Msg::operator[](int channel){
    this->channel = channel;
    return *this;
}

int main(int argc,const char** argv){
    Msg m("./ipc");
    m[1] << "helloworld";
    string str;
    m[1] >> str;
    cout << str << endl;
}

4.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

using namespace std;                 

class Sem{
private:
    key_t key;
    int id;
    int index;
public:
    Sem(int x,int y)
    {
        key = ftok("./ipc",2);
        id = semget(key,x,IPC_CREAT | 0666);
        for(int i=0;i<x;i++)
        {
            semctl(id,i,SETVAL,y);
        }
    }

    void init(int y)
    {
        semctl(id,index,SETVAL,y);
    }

    Sem& operator-(int y)
    {
        struct sembuf buf = {0};
        buf.sem_num = index;
        buf.sem_op = -abs(y);
        buf.sem_flg = SEM_UNDO;
        semop(id,&buf,1);
        return *this;
    }

    Sem& operator+(int y)
    {
        struct sembuf buf = {0};
        buf.sem_num = index;
        buf.sem_op = abs(y);
        buf.sem_flg = SEM_UNDO;
        semop(id,&buf,1);
        return *this;
    }
    Sem& operator++()
    {
        return *this+1;
    }
    Sem& operator--()
    {
        return *this-1;
    }
    Sem& operator[](int y)
    {
        index = y;
        return *this;
    }

    ~Sem()
    {
        semctl(id,1,IPC_RMID);
    }
};

int main(int argc,const char** argv)
{
    Sem s(3,0);
    s[1].init(10);
    s[1] + 1;
    s[1] - 1;
    
}


网站公告

今日签到

点亮在社区的每一天
去签到