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;
}
相关推荐
季明洵18 分钟前
反转字符串、反转字符串II、反转字符串中的单词
java·数据结构·算法·leetcode·字符串
2401_8414956422 分钟前
【Python高级编程】近似串匹配
python·算法·动态规划·字符串·数组·时间复杂度·空间复杂度
lingggggaaaa27 分钟前
安全工具篇&魔改二开&CheckSum8算法&Beacon密钥&Stager流量&生成机制
学习·算法·安全·web安全·网络安全·免杀对抗
Python+JAVA+大数据29 分钟前
SQL玩出算法竞赛高度!郑凌云数独算法:递归CTE+位运算DFS回溯全解析
数据库·sql·算法·搜索引擎·深度优先·dfs
MicroTech202531 分钟前
量子主成分分析(QPCA):微算法科技(NASDAQ :MLGO)重构图像降维与特征提取的技术
科技·算法·重构
历程里程碑33 分钟前
滑动窗口------滑动窗口最大值
大数据·python·算法·elasticsearch·搜索引擎·flask·tornado
Mr_Xuhhh34 分钟前
C语言字符串与内存操作函数模拟实现详解
java·linux·算法
B站_计算机毕业设计之家37 分钟前
AI大模型:Deepseek美食推荐系统 机器学习 协同过滤推荐算法+可视化 Django框架 大数据毕业设计(源码)✅
python·算法·机器学习·数据分析·django·推荐算法·美食
小草cys38 分钟前
基于大模型的图像目标检测及跟踪算法
人工智能·算法·目标检测
代码游侠1 小时前
C语言核心概念复习(三)
开发语言·数据结构·c++·笔记·学习·算法