作业day6
1:编写一个长方形类, 私有成员 a,b 构造函数初始化 set get 接口 编写一个正方形类,继承自长方形类 构造函数初始化 无论如何,正方形类对象总是正方形的
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class Data{
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;}
};
class AA:public AB{
public:
AA(int ab)
:AB(a,b){}
void setA(int a){
AB::setA(a);
AB::setB(a);
}
void setB(int b){
AB::setA(a);
AB::setB(a);
}
};
int main(int argc,const char** argv){
AA aa;
aa.setA(4);
aa.setB(5);
}
写一个三角形类,拥有私有成员 a,b,c 三条边 写好构造函数初始化 abc 以及 abc 的set get 接口 再写一个等腰三角形类,继承自三角形类 1:写好构造函数,初始化三条边 2:要求无论如何,等腰三角形类对象,总是等腰的 再写一个等边三角形类,继承自等腰三角形类 1:写好构造函数,初始化三条边 2:要求无论如何,等腰三角形类对象,总是等边
#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(a);
ABC::setB(a);
}
};
class AAA : public AAC {
public:
AAC(int abc = 0) : AAC(abc,abc,abc ) {}
void setA(int a) {
ABC::setA(a);
ABC::setB(a);
}
void setB(int b) {
ABC::setA(a);
ABC::setB(a);
}
void setC(int c) {
ABC::setA(a);
ABC::setB(a);
}
};
int main(int argc, const char** argv) {
AAC aac; // 等腰三角形对象
aac.setA(4);
aac.setB(5);
aac.setC(6);
AAA aaa;
aaa.setA(2);
}
封装消息队列 class Msg{ key_t key int id; int channel } 实现以下功能 Msg m("文件名") m[1].send("数据"),将数据发送到1号频道中 string str = m[1].recv(int size) 从1号频道中读取消息,并且返回 把 send 改成 operator<< ,recv 改成 operator>> 实现效果: m[1] << "helloworld" 将 "helloworld" 写入消息队列的1号频道中 m[1] >> str 读取消息队列中1频道中的消息,存入 str 中 编写程序测试
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
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){
Msg& operator<<(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){
Msg& operator>>(string& str) {
msgbuf buf = {0};
msgrecv(id,&buf,size,channel,0);
string str = buf.text;
return str;
}
friend Msg operator[](const Msg& l,int channel);
};
// m[1].send(str);
Msg& operator[](const Msg& l,int channel){
l.channel = channel;
return l;
}
int main(int argc,const char** argv){
}
封装信号灯集 class Sem{ key_t key int id; int index } 实现以下功能 Sem s(参数x,参数y):创建信号灯集,信号灯集中存在 x 个信号量,并且将所有信号量初始化为 y s[1].init(10):手动初始化信号灯集中的第1个信号量,初始化成 10 s[1] + 1 让信号灯集中的第1个信号量的值 +1 s[1].operator+(1) s[1] - 1 让信号灯集中的第1个信号量的值 -1 追加 operator ++ 功能,即解锁一次 以及 operator-- 功能, 即上锁一次 编写程序测试
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class Sem{
private:
key_t key;
int id;
int index;
public:
Sem(const string& filename = "",int n,int val){
key = ftok(filename.data());
id = semget(key,n,IPC_CREAT | 0666);
for(int i=0;i<n;i++){
semctl(id,i,SETVAL,val);
}
}
~Sem(){
semctl(id,0,IPC_RMID);
}
friend Sem& operator+(const Sem& l,int val);
friend Sem& operator-(const Sem& l,int val);
friend Sem operator[](const Sem& l,int index);
friend Sem& operator++(Sem& l);
friend Sem& operator--(Sem& l);
};
// Sem s
// s + 1解锁
// s - 1 上锁
// s + 1 + 1 + 1 - 2 - 3
// int(4) + 3
Sem& operator+(const Sem& l,int val){
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = abs(val);
buf.sem_flg = SEM_UNDO;
semop(id,&buf,1);
return l;
}
/*
Sem s;
s[0] - 1 s.index = 0确定好了
*/
Sem& operator-(const Sem& l,int val){
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = -abs(val);
buf.sem_flg = SEM_UNDO;
semop(id,&buf,1);
return l;
}
Sem& operator++(Sem& l) {
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
semop(l.id, &buf, 1);
return l;
}
Sem& operator--(Sem& l) {
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
semop(l.id, &buf, 1);
return l;
}
Sem& operator[](const Sem& l,int index){
l.index = index;
return l;
}
int main(int argc,const char** argv){
Sem s("test", 1, 1);
++s[0];
cout << "++: " << s[0] << endl;
--s[0];
cout << "--: " << s[0] << endl;
}