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;
}
相关推荐
拾忆-eleven16 分钟前
C++算法(19):整数类型极值,从INT_MIN原理到跨平台开发实战
数据结构·c++·算法
JK0x0735 分钟前
代码随想录算法训练营 Day39 动态规划Ⅶ 打家劫舍
算法·动态规划
blammmp1 小时前
算法专题四:前缀和
java·开发语言·算法
望未来无悔2 小时前
系统学习算法:动态规划(斐波那契+路径问题)
java·算法
明月看潮生2 小时前
青少年编程与数学 02-018 C++数据结构与算法 25课题、图像处理算法
c++·图像处理·算法·青少年编程·编程与数学
我是一只鱼02232 小时前
LeetCode算法题 (反转链表)Day17!!!C/C++
数据结构·c++·算法·leetcode·链表
LuckyLay2 小时前
LeetCode算法题(Go语言实现)_62
算法·leetcode·职场和发展
元亓亓亓2 小时前
LeetCode热题100--54.螺旋矩阵--中等
算法·leetcode·矩阵
菜鸟破茧计划2 小时前
C++ 算法学习之旅:从入门到精通的秘籍
c++·学习·算法
灏瀚星空3 小时前
PyTorch 入门与核心概念详解:从基础到实战问题解决
人工智能·pytorch·python·深度学习·算法·机器学习