第十一站:多态练习ODU

实现动态切换

ODU.h

#pragma once
#include <iostream>
using namespace std;
#define ODU_TYPE_311_FLAG "311"
#define ODU_TYPE_335_FLAG "335"
enum class ODU_TYPE {
	ODU_TYPE_311,
	ODU_TYPE_335,
	ODU_TYPE_UNKNOW
};

class ODU{
public:
	ODU();
	//发射频率
	virtual bool setTxFreq(int);
	virtual int getTxFreq();
	//接收频率
	virtual bool setRxFreq(int);
	virtual int getRxFreq();
	//发射功率
	virtual bool setTxPower(float);
	virtual float getTxPower();
	//获取接收电平
	virtual float getRxl();
	//获取心电包
	virtual bool hearBeat();
	//获取设备的名称
	virtual string name();
	//获取设备类型
	virtual ODU_TYPE getOduType();
protected:
	int TxFreq;
	int RxFreq;
	float TxPower;
	float rxl;
	ODU_TYPE type;
};

ODU335.h

#pragma once
#include "ODU.h"
class ODU335 :public ODU{
public:
	ODU335();
	//重写父类heat
	bool hearBeat();
};

ODU.CPP

#include "ODU.h"


/*
	int TxFreq;
	int RxFreq;
	float TxPower;
	float rxl;
	ODU_TYPE type;
*/
ODU::ODU(){
	TxFreq = 34100;
	RxFreq = 31000;
	TxPower = 40;
	rxl = 0;
	type = ODU_TYPE::ODU_TYPE_311;
	cout << "调用老设备" << endl;
}

bool ODU::setTxFreq(int frequence)
{
	TxFreq = frequence;
	cout << name() << "设置发射频率为:" << TxFreq << "Hz" << endl;
	return true;
}

int ODU::getTxFreq()
{
	return TxFreq;
}

bool ODU::setRxFreq(int frequence)
{
	RxFreq = frequence;
	cout << name() << "设置接收频率为:" << RxFreq << "Hz" << endl;
	return true;
}

int ODU::getRxFreq()
{
	return RxFreq;
}

bool ODU::setTxPower(float power)
{
	TxPower = power;
	cout << name() << "设置发射功率为:" << TxPower << "dpm" << endl;
	return true;
}

float ODU::getTxPower()
{
	return TxPower;
}

float ODU::getRxl()
{
	return rxl;
}

bool ODU::hearBeat()
{
	cout << name() << "模拟串口协议读取数据:获取心跳包的反馈...[" << ODU_TYPE_311_FLAG
		<< "]";
	string response;
	cin >> response;
	bool ret = false;
	if (response == ODU_TYPE_311_FLAG) {
		type = ODU_TYPE::ODU_TYPE_311;
		ret = true;
	}
	return ret;
}

string ODU::name()
{
	string ret;
	switch (type)
	{
	case ODU_TYPE::ODU_TYPE_311:
		ret = "ODU311";
		break;
	case ODU_TYPE::ODU_TYPE_335:
		ret = "ODU335";
		break;
	case ODU_TYPE::ODU_TYPE_UNKNOW:
	default:
		ret = "ODU_UNKNOW";
		break;
	}
	return ret;
}

ODU_TYPE ODU::getOduType()
{
	return type;
}

ODU335.CPP

#include "ODU335.h"
#include "ODU.h"
ODU335::ODU335()
{
	cout << "调用ODU335" << endl;
	type = ODU_TYPE::ODU_TYPE_335;

}

bool ODU335::hearBeat()
{
	cout << name() << "模拟串口协议读取数据:获取心跳包的反馈...[" << ODU_TYPE_335_FLAG
		<< "]";
	string response;
	cin >> response;
	bool ret = false;
	if (response == ODU_TYPE_335_FLAG) {
		type = ODU_TYPE::ODU_TYPE_335;
		ret = true;
	}
	return ret;
}

main.cpp

#include <iostream>
#include <Windows.h>
#include <thread>
#include "ODU.h"
#include "ODU335.h"
using namespace std;
ODU* odu = NULL;
void test(ODU* odu) {
	odu->setTxFreq(35000);
	cout << odu->getRxFreq() << endl;
	odu->hearBeat();
}
void oduMonitorHandler() {
	while (1){
		if (odu->hearBeat() == false) {
			//切换odu
			ODU_TYPE type = odu->getOduType();
			switch (type) {
			case ODU_TYPE::ODU_TYPE_311:
				delete odu;
				odu = new ODU335;
				break;
			case ODU_TYPE::ODU_TYPE_335:
				delete odu;
				odu = new ODU;
				break;
			default:
				odu = NULL;
				return;
			}
		}
	}
}
int main(void) {
	odu = new ODU();
	//创建一个线程
	thread oduMonitor(oduMonitorHandler);
	//等待主线程结束
	oduMonitor.join();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值