Create a class called Transmitter. It has three unsigned int private data members: pay_load,
parity and data_packet; It has one function called parity_bit(). It calculates parity bit by XORing
all pay_load bits and assign the value to parity. By using function get pay_load(), pay_load is got.
data_packet is calculated by multiplying parity_bit with 32768 and add the product with (pay_load
– 32768). Create another class called Receiver. It has unsigned int private data members Rx_Pkt
, Rx_Data and E_Flag. Rx_Data is got from Rx_Data as given below. First E_Flag is calculated
from Rx_Pkt by XORing all bits. If E_Flag is zero, then Rx_Data = Rx_Pkt – 32768, Else a
message should warn about corrupted packet. Derive a class called Tranceiver from the above two
classes and check the functionalities. Use any other functions if needed.
#include <iostream>
#include <vector>//use for stored bits number
#include <algorithm>//use reverse funtion
using namespace std;
typedef unsigned int ui;
//Implement class transmitter
class Transmitter
{
private:
ui pay_load;//Pay_load private member
ui parity;
ui data_packet;
//Function which return count '1' bit in number
//for example 8=1000->1-'1' bit
ui cntOne(ui num)
{
//convert to binary and calculate count 1
ui ans = 0;
while (num)
{
ans += (num % 2);
num /= 2;
}
return ans;
}
public:
Transmitter(ui py_load)
{
this->pay_load = py_load;
}
//get_pay_load()
ui getPay_Load()const
{
return this->pay_load;
}
//function parity_bit()
ui parity_bit()
{
int cnt = cntOne(this->getPay_Load());
if (cnt % 2)
{
this->parity = 1;
return 1;
}
else
{
this->parity = 0;
return 0;//Xoring Parity bit =0 for example 101->3-1 bit return 1
}
}
//calcelate date_packet parity*32768+(pay_load-32768)
void CalcDatePack()
{
this->data_packet=this->parity_bit() * 32768 + (getPay_Load() - 32768);
}
ui get_DatPack()
{
return this->data_packet;
}
};
//Implement class Receiver
class Receiver
{
private:
ui Rx_Pkt;
ui Rx_Data;
ui E_Flag;
public:
//Constructor
Receiver(ui rx_pkt)
{
this->Rx_Pkt = rx_pkt;
}
bool doIt()
{
vector<int>bts;//Bits
int num = this->Rx_Pkt;
while (num)
{
bts.push_back(num % 2);
num /= 2;//convert to binary
}
//reverse
reverse(bts.begin(), bts.end());
ui xr = bts[0];
for (int i = 1; i < bts.size(); i++)
{
//Xor all bits
xr ^= bts[i];
}
this->E_Flag = xr;
if (this->E_Flag == 0)
{
this->Rx_Data = Rx_Pkt - 32768;
return true;
}
else
cout << "Warning correpted packet!!!\n";
return false;
}
//Get method return member rx_data
ui getRxData()
{
return this->Rx_Data;
}
};
//Implement class Tranceiver inheritance both class up
class Tranceiver :public Transmitter, public Receiver
{
public:
Tranceiver(ui pyl, ui rx_pk) :
Transmitter(pyl), Receiver(rx_pk)
{
//Testing
cout << "Parity_Bit=" << this->parity_bit() << endl;
//Calc dat_pack
this->CalcDatePack();
cout << "data_packet=" << this->get_DatPack() << endl;
//Calc Rx_Data
if (this->doIt())
{
cout << "Rx_Data: " << this->getRxData() << endl;
}
}
};
int main()
{
Tranceiver(1502, 2548);
//or you can get data from the console
return 0;
}
Comments
Leave a comment