c++day4

作业

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

using namespace std;                 

class S{
private:
	int a;
	int b;
public:
	S(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;}
};

class Z:public S
{
public:
	Z(int ab=0)
	:S(ab,ab)
	{}
	void setA(int a)
	{
		S::setA(a);
		S::setB(a);
	}
	void setB(int b)
	{
		S::setA(b);
		S::setB(b);
	}
};

int main(int argc,const char** argv)
{
	Z ab;
	ab.setA(3);
	ab.setB(4);
}
复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

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;}
};

class AAC:public ABC
{
public:
	AAC(int ab=0,int c=0)
		:ABC(ab,ab,c)
	{}

	void setA(int a)
	{
		ABC::setA(a);
		ABC::setB(a);
	}

	void setB(int b)
	{
		ABC::setA(b);
		ABC::setB(b);
	}

	void setC(int c)
	{
		ABC::setA(c);
		ABC::setB(c);
	}
};

class AAA:public AAC
{
public:
	AAA(int ab=0)
		:AAC(ab,ab)
	{}

	void setA(int a)
	{
		AAC::setA(a);
		AAC::ABC::setC(a);
	}
	
	void setB(int b)
	{
		AAC::setB(b);
		AAC::ABC::setC(b);
	}

	void setC(int c)
	{
		AAC::setC(c);
		AAC::ABC::setC(c);
	}
};

int main(int argc,const char** argv)
{
	AAC aac;
	aac.setA(4);
	aac.setB(5);
	aac.setC(6);

	AAA aaa;
	aaa.setA(5);
	aaa.setB(7);
	aaa.setC(13);
}
复制代码
#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;
}
复制代码
#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;
	
}
相关推荐
csdn_aspnet1 小时前
C++ 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
c++·算法
代码民工正电荷3 小时前
JAVA重要知识点和集合
java·开发语言
星恒随风3 小时前
C++11 加餐(4)并发支持库——condition_variable 条件变量与生产者消费者模型
开发语言·c++
利威尔xu3 小时前
Go map 并发十连问:多 goroutine 同时读写触发 fatal error,进程直接崩溃
开发语言·golang
你不知道我是谁?3 小时前
GESP C++ 补充知识:浮点数格式化输出(%f/%e/%g)与 fmod 浮点余数实战
开发语言·c++
九蝶科技4 小时前
运费分摊的二开实现:BigDecimal 精确分摊 + 尾差补齐 + 暂估调整
开发语言·python
Ryan-Zhong4 小时前
一次接入 16 个平台:视频链接元数据解析 API 的 Python 实战与踩坑记录
开发语言·python·dreamweaver
Rabitebla5 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
无限聚核6 小时前
PHP 对接美团、抖音团购核销接口:预核销、核销、重试和撤销一份代码跑通,无限聚核
开发语言·php·美团·团购核销接口
WL学习笔记7 小时前
继承(C++)
c++·继承