两个设备之间的转换(多态性练习)

要求:
有两个设备,一个设备正常运行,所有出现故障,立马转换到里一个设备。
因为原来已经定义好一个类了,所以直接用了继承
原来的设备:
ODU.h

#pragma once
#include<string>
#define ODU_TYPE_331_FLAG "331"
#define ODU_TYPE_335_FLAG "335"
using namespace std;
enum class ODU_TYPE {
 ODU_TYPE_331,
 ODU_TYPE_335,
 ODU_TYPE_UNKNOWN
};
class ODU
{
public:
	ODU();
	virtual int getTxFre();//获取发射频率
	virtual bool setTxFre(int );//设置发射频率

	virtual int getRxFre();//获取接收频率
	virtual bool setRxFre(int);//获取接收频率

	virtual float getTxPower();//获取发射功率
	virtual bool setTxPower(float);//设置发射功率

	virtual float getRxL();//获取电平

	virtual bool heartBeat();//心跳包
	virtual string name();//获取该设备的名称
	virtual ODU_TYPE getODUType();//获取当前ODU的类型
protected:
	int txFre;//发射频率
	int rxFre;//接收频率
	float txPower;//发射功率
	float rxL;//接收电平
	ODU_TYPE type;
};


ODU.cpp

#include "ODU.h"
#include<iostream>
#include<string>
using namespace std;
ODU::ODU() {
	txFre = 34400;
	rxFre = 31100;
	txPower = 20;
	rxL = 0;
	type = ODU_TYPE::ODU_TYPE_331;
	cout << "调用ODU()" << endl;
}
int ODU::getTxFre()
{
	return txFre;
}
bool ODU::setTxFre(int frequence)
{
	txFre = frequence;
	cout << name() << "发射功率已经设置为" << txFre << "HZ" << endl;
	return true;
}
int ODU::getRxFre()
{
	return rxFre;
}
bool ODU::setRxFre(int frequence)
{
	rxFre = frequence;
	cout << name() << "就收频率已经设置为" << rxFre << "HZ" << endl;
	return true;
}
float ODU::getTxPower()
{
	return txPower;
}
bool ODU::setTxPower(float power)
{
	txPower = power;
	cout << name() << "发射功率已经设置为" << txPower<<"dBm" << endl;

	return true;
}
float ODU::getRxL()
{
	return rxL;
}
bool ODU::heartBeat()
{
	cout << name() << "模拟串口协议读取数据:获取心跳包的反馈......【"
		<< ODU_TYPE_331_FLAG << "】";
	bool ret = false;
	string response;
	cin >> response;
	if (response == ODU_TYPE_331_FLAG) {
		type = ODU_TYPE::ODU_TYPE_331;
		ret = true;
	}
	return ret;
}
string ODU::name()
{
	string ret;
	switch (type) {
	case ODU_TYPE::ODU_TYPE_331:
		ret = "ODU331";
		break;
	case ODU_TYPE::ODU_TYPE_335:
		ret = "ODU335";
		break;
	case ODU_TYPE::ODU_TYPE_UNKNOWN:
	default:
		ret = "ODU_UNKNOW";
		break;
	}
	return ret;
}
ODU_TYPE ODU::getODUType()
{
	return type;
}

然后下面的设备继承了这个设备,并把心跳包函数重写了
ODU335.h

#pragma once
#include"ODU.h"
#include<iostream>
class ODU335:public ODU
{
public:
	ODU335();
	bool heartBeat()override;
private:
};

ODU335.cpp

#include "ODU335.h"
using namespace std;
ODU335::ODU335()
{
	cout << "调用ODU335()" << endl;
	type = ODU_TYPE::ODU_TYPE_335;
}
bool ODU335::heartBeat()
{
	cout << name() << "模拟串口协议读取数据:获取心跳包的反馈.....【"
		<< ODU_TYPE_335_FLAG << "】";
	bool ret = false;
	string response;
	cin >> response;
	if (response == ODU_TYPE_335_FLAG) {
		type = ODU_TYPE::ODU_TYPE_335;
		ret = true;
	}
	return ret;
}

接下来写主函数:
这里刚学了一个叫进程的东西
用进程来实现,多进程就是同时跑多个任务,所以在主函数中加一个进程,用来一直测试心跳包,及设备是否一直正常运行,
其次这个测试函数
就是判断设备是否正常,如果不正常就直接换到另一个设备
:上源码:

#include"ODU.h"
#include"ODU335.h"
#include<thread>
#include<iostream>
#include<Windows.h>
using namespace std;
ODU* odu = NULL;
void test() {
	while (1) {
		if (odu->heartBeat() == false) {
			ODU_TYPE type = odu->getODUType();
			switch (type) {
			case ODU_TYPE::ODU_TYPE_331:
				delete odu;
				odu = new ODU335;
				break;
			case ODU_TYPE::ODU_TYPE_335:
				delete odu;
				odu = new ODU;
				break;
			default:
				return;
			}
		}
		Sleep(3000);
	}
}
int main() {
	odu = new ODU();
	test();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值