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;
}
相关推荐
永恒迷星.by22 分钟前
全球变暖(蓝桥杯 2018 年第九届省赛)
算法
旧时光林1 小时前
蓝桥杯 分解质因数(唯一分解定理)
数据结构·c++·算法·蓝桥杯·模拟·枚举
烁3471 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐
Hello bugyan2 小时前
并查集initial,find,union+应用
数据结构·算法
chase。2 小时前
机器人零位标定修正流程介绍
人工智能·算法·机器人
yumuing2 小时前
融合动态权重与抗刷机制的网文评分系统——基于优书网、IMDB与Reddit的混合算法实践
后端·算法·架构
知星小度S2 小时前
算法训练之动态规划(四)——简单多状态问题
算法·动态规划
亓才孓2 小时前
[leetcode]差分算法
算法
ClaNNEd@3 小时前
尚硅谷Java第 4、5 章IDEA,数组
java·算法
dokii13 小时前
leetcode589 N叉树的前序遍历
算法