02.24 运算符重载函数和继承相关练习

cpp 复制代码
#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;
}
cpp 复制代码
#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;
}
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;

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;
}
cpp 复制代码
#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;
}
相关推荐
电子_咸鱼15 分钟前
常见面试题——滑动窗口算法
c++·后端·python·算法·leetcode·哈希算法·推荐算法
mit6.82431 分钟前
hash+presum判等|幻方0
算法
萌>__<新1 小时前
力扣打卡每日一题————最小覆盖子串
数据结构·算法·leetcode·滑动窗口·哈希表
ada7_1 小时前
LeetCode(python)230.二叉搜索树中第k小的元素
python·算法·leetcode·链表
TL滕2 小时前
从0开始学算法——第十五天(滑动窗口练习)
笔记·学习·算法
DuHz2 小时前
milliLoc 论文精读:把商用毫米波 FMCW 的绝对测距从“厘米栅格”推进到“毫米级连续值”,并顺带修正 AoA 的系统相位偏差
论文阅读·物联网·算法·信息与通信·毫米波雷达
qq_401700412 小时前
Linux文件锁解决多进程并发
linux·服务器·算法
长安er2 小时前
LeetCode 83/237/82 链表删除问题-盒子模型
数据结构·算法·leetcode·链表·力扣
小虎牙0073 小时前
RSA 的核心原理
算法
重生之后端学习3 小时前
56. 合并区间
java·数据结构·后端·算法·leetcode·职场和发展