1.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class Rectangle{
private:
int a;//长
int b;//宽
public:
Rectangle(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 Square:public Rectangle{
public:
Square(int k=0):Rectangle(k,k){}
void setA(int a){
Rectangle::setA(a);
Rectangle::setB(a);
}
void setB(int a){
Rectangle::setA(a);
Rectangle::setB(a);
}
};
int main(int argc,const char** argv){
Square g;//正方形对象
g.setA(5);
cout << g.getA() << " " << g.getB() << endl;
g.setB(6);
cout << g.getA() << " " << g.getB() << endl;
}
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 a){
ABC::setA(a);
ABC::setB(a);
}
};
class AAA:public AAC{
public:
AAA(int abc=0):AAC(abc,abc){}
void setA(int a){
AAC::setA(a);
ABC::setC(a);
}
void setB(int a){
AAC::setA(a);
ABC::setC(a);
}
void setC(int a){
AAC::setA(a);
ABC::setC(a);
}
};
int main(int argc,const char** argv){
AAC aac;
aac.setA(4);
cout << aac.getA() << " " << aac.getB() << " " << aac.getC() << endl;
aac.setB(5);
cout << aac.getA() << " " << aac.getB() << " " << aac.getC() << endl;
aac.setC(6);
cout << aac.getA() << " " << aac.getB() << " " << aac.getC() << endl;
AAA aaa;
aaa.setA(3);
cout << aaa.getA() << " " << aaa.getB() << " " << aaa.getC() <<endl;
aaa.setB(4);
cout << aaa.getA() << " " << aaa.getB() << " " << aaa.getC() <<endl;
aaa.setC(5);
cout << aaa.getA() << " " << aaa.getB() << " " << aaa.getC() <<endl;
}
3.
#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;
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);
}
// 重载 << 运算符用于发送消息
Msg& operator<<(const string& str) {
msgbuf buf = {0};
strcpy(buf.text, str.data());
buf.channel = channel;
msgsnd(id, &buf, strlen(buf.text) + 1, 0);
return *this;
}
// 重载 >> 运算符用于接收消息
Msg& operator>>(string& str) {
msgbuf buf = {0};
msgrcv(id, &buf, sizeof(buf.text), channel, 0);
str = buf.text;
return *this;
}
// 重载 [] 运算符用于选择频道
Msg& operator[](int channel) {
this->channel = channel;
return *this;
}
};
int main(int argc, const char** argv) {
Msg m("msgfile");
// 发送消息到1号频道
m[1] << "helloworld";
// 从1号频道接收消息
string str;
m[1] >> str;
cout << "接收到消息: " << str << endl;
return 0;
}
4.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.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 = 1, int val = 0){
key = ftok(filename.data(), 'a');
id = semget(key, n, IPC_CREAT | 0666);
for(int i = 0; i < n; i++){
semctl(id, i, SETVAL, val);
}
index = 0;
}
// 析构函数
~Sem(){
semctl(id, 0, IPC_RMID);
}
// 声明友元运算符重载函数
friend Sem& operator+(Sem& l, int val);
friend Sem& operator-(Sem& l, int val);
// 后置 ++ 运算符重载
Sem operator++(int) {
Sem temp = *this;
sembuf buf = {0};
buf.sem_num = index;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
semop(id, &buf, 1);
return temp;
}
// 后置 -- 运算符重载
Sem operator--(int) {
Sem temp = *this;
sembuf buf = {0};
buf.sem_num = index;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
semop(id, &buf, 1);
return temp;
}
Sem& operator[](int index) {
this->index = index;
return *this;
}
};
// s + val 解锁操作
Sem& operator+(Sem& l, int val){
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = abs(val);
buf.sem_flg = SEM_UNDO;
semop(l.id, &buf, 1);
return l;
}
// s - val 上锁操作
Sem& operator-(Sem& l, int val){
sembuf buf = {0};
buf.sem_num = l.index;
buf.sem_op = -abs(val);
buf.sem_flg = SEM_UNDO;
semop(l.id, &buf, 1);
return l;
}
int main(int argc, const char** argv){
Sem s("test", 1, 1);
s++; // 调用后置 ++ 运算符
s--; // 调用后置 -- 运算符
s[0] + 1; // 使用 operator[]
return 0;
}