当前位置: 首页 > news >正文

C++ 继承与运算符重载的简单练习

 1.长方形的继承类

 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;                 

class AB{
private:
	int a;
	int b;
public:
	AB(int a=0,int b=0){}
	void seta(int l){a=l;}
	void setb(int l){b=l;}
	int geta(){return a;}
	int getb(){return b;}
};

class AA:public AB{
	
	public:
		AA(int ab=0,int bc=0)
		:AB(ab,bc){}
		void setA(int a)
		{
			seta(a);
			setb(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
		}

		void show()
		{
			if(geta()==getb())
			{
				cout<<"是正方形"<<endl;
			}else
			{
				cout<<"是长方形"<<endl;
			}
		}
};

int main(int argc,const char** argv){
	
	AA aa;
	aa.setA(4);
	aa.setB(5);
	aa.show();
	return 0;
}

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){}
	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 AAB:public ABC{
	public:
		AAB(int ab=0,int bc=0,int cd=0)
		:ABC(ab,bc,cd){}
		void setA(int a)
		{
			seta(a);
			setb(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
		}
		void setC(int a)
		{
			seta(a);
			setb(a);
		}
	};


class AAA:public AAB{
	public:
		AAA(int ab=0,int bc=0,int cd=0)
		:AAB(ab,bc,cd){}
		void setA(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}
		void setB(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}
		void setC(int a)
		{
			seta(a);
			setb(a);
			setc(a);
		}

		void show()
		{
			cout<<geta()<<endl<<getb()<<endl<<getc()<<endl;

			if(geta()==getb()&&geta()==getc())
			{
				cout<<"是等边三角形"<<endl;
			}else
			{
				cout<<"不是等边三角形"<<endl;
			}
		}
};

int main(int argc,const char** argv){
	
	AAA aaa;
	aaa.AAA::setA(3);
	aaa.AAA::setB(4);
	aaa.AAA::setC(5);
	aaa.AAA::show();
	return 0;
}

 3.使用<<  与  >>运算符的消息队列

 

 queue.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; 

struct MessageBuf {
    long mtype;     
    char mtext[128];
};

class Msg{
private:
	long channel;
	MessageBuf buf;
	key_t key;
	int id;
public:
	Msg(const char* filename);
	Msg& operator[](int c);
//	void send(const char* txt);
	friend void operator<<(Msg& buf,const char* txt);
};


Msg::Msg(const char* filename)
{
	key=ftok(filename,1);
	id=msgget(key,IPC_CREAT|0666);
	memset(&buf,0,128);
}


Msg& Msg::operator[](int c)
{
	channel=c;	
	return *this;
}

void operator<<( Msg& m,const char* txt)
{
	m.buf.mtype=m.channel;
	strcpy(m.buf.mtext,txt);
	msgsnd(m.id,&m.buf,sizeof(m.buf.mtext),IPC_NOWAIT);
}


int main(int argc,const char** argv){

while(1)
{
	Msg m("ipc");
	long channel;
	cout<<"请选择频道号: ";
	cin>>channel;
	m[channel]<<"hello world";
}
	return 0;
}

queue_read.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;                

struct MessageBuf {
    long mtype;     
    char mtext[128];
};

class Msg{
private:
	long channel;
	MessageBuf buf;
	key_t key;
	int id;
public:
	Msg(const char* filename);
	Msg& operator[](int c);
	friend Msg& operator>>(Msg& m,string& s);
};


Msg::Msg(const char* filename)
{
	key=ftok(filename,1);
	id=msgget(key,IPC_CREAT|0666);
	memset(&buf,0,128);
}


Msg& Msg::operator[](int c)
{
	channel=c;	
	return *this;
}


Msg& operator>>(Msg& m,string& s)
{
	
	m.buf.mtype=m.channel;
	msgrcv(m.id,&m.buf,sizeof(m.buf.mtext),m.channel,IPC_NOWAIT);
	s=m.buf.mtext;
	return m;
}

int main(int argc,const char** argv){
while(1)
{
	Msg m("ipc");
	long channel;
	cout<<"请选择频道号: ";
	cin>>channel;
	string str;
	m[channel]>>str;
	cout<<str<<endl;
}
	
}

 4.运算符重载  ++  --  信号灯集

 

lude <sys/ipc.h>
#include <semaphore.h>
#include <sys/sem.h>

using namespace std;

class Sem{
private:
	key_t key;
	int id;
	int index;
public:
	Sem(const char* filename,int n,int val){
		key = ftok(filename,1);
		id = semget(key,n,IPC_CREAT | 0666);
		for(int i=0;i<n;i++){
			semctl(id,i,SETVAL,val);
		}
	}

	~Sem(){
		semctl(id,0,IPC_RMID);
	}

	friend Sem& operator+(Sem& l,int val);
	friend Sem& operator-(Sem& l,int val);
	Sem& operator[](int index);

	Sem& operator++(int);
	Sem& operator--(int);
};


// Sem s
// s + 1解锁
// s - 1 上锁
// s + 1 + 1 + 1 - 2 - 3
// int(4) + 3
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;
}

Sem& Sem::operator++(int)
{
	sembuf buf = {0};
	buf.sem_num =this->index;
	buf.sem_op = 1;
	buf.sem_flg = SEM_UNDO;
	semop(this->id,&buf,1);
	return *this;
}



/*
	Sem s;
	s[0] - 1  s.index = 0确定好了
*/

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;                    
}


Sem& Sem::operator--(int)
{
	sembuf buf = {0};
    buf.sem_num = this->index;
    buf.sem_op = -1;
    buf.sem_flg = SEM_UNDO;
    semop(this->id,&buf,1); 
	return *this; 
}


Sem& Sem::operator[](int index){
	this->index = index;
	return *this;
}

相关文章:

  • LabVIEW不规则正弦波波峰波谷检测
  • 网站快速收录:如何优化网站图片Alt标签?
  • javascript实现二进制、十进制、十六进制和八进制之间的相互转换
  • 短剧小程序系统源码
  • 大模型做导师之开源项目学习(lightRAG)
  • 当G1机器人跳出“丝滑舞步“:算力+AI 催生具身智能
  • Entity Framework Core开发采用Repository与DbContext优势和劣势,及开发的方便与快速
  • 在虾分发平台上将h5页面打包成app
  • 基于springboot的学习社区博客
  • 关于uniapp使用renderJS中调用父类方法和参数的使用
  • dubbo转http方式调用
  • 爱普生SG-8101CE可编程晶振赋能智能手机的精准心脏
  • 6.3 - UART串口数据发送之中断
  • MessageAuthenticator
  • 设计模式-(单例,简单工厂,工厂,抽象工厂)
  • 【组态PLC】基于三菱西门子S7-200PLC和组态王自动洗衣机组态设计【含PLC组态源码 M012期】
  • [创业之路-326]:两种事业部授权模式:战略管控与运营管控
  • 数位dp-
  • el-select滚动获取下拉数据;el-select滚动加载
  • 【信息系统项目管理师-案例真题】2011上半年案例分析答案和详解
  • 上海将建设万兆小区、园区及工厂,为模型训练数据的传输提供硬件支持
  • 把中国声音带向世界,DG和Blue Note落户中国
  • 普京批准俄方与乌克兰谈判代表团人员名单
  • 陕西宁强县委书记李宽任汉中市副市长
  • 外交部:各方应为俄乌双方恢复直接对话创造条件
  • 《歌手2025》公布首发阵容,第一期就要淘汰一人