第十一站:多态练习ODU

发布时间:2024年01月20日

实现动态切换

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

文章来源:https://blog.csdn.net/qq_48397625/article/details/135721797
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。