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>
using namespace std;
class Transmitter{
private:
unsigned int pay_load;
unsigned int parity;
unsigned int data_packet;
public:
Transmitter(){}
Transmitter(unsigned int pay_load){
this->pay_load = pay_load;
}
// It calculates parity bit by XORing all pay_load bits and assign the value to parity.
void parity_bit()
{
unsigned int result = pay_load ^ (pay_load >> 1);
result = result ^ (result >> 2);
result = result ^ (result >> 4);
result = result ^ (result >> 8);
result = result ^ (result >> 16);
if (result & 1){
parity=1;
}else{
parity=0;
}
cout<<"\nParity is "<<parity<<endl;
}
//By using function get pay_load(), pay_load is got.
void get_pay_load()
{
cout<<"Enter pay load: ";
cin>>pay_load;
}
//data_packet is calculated by multiplying parity_bit with 32768 and add the product with (pay_load – 32768).
void dataPacket()
{
data_packet = (parity*32768) + (pay_load-32768);
cout<<"Data packet is: "<<data_packet<<endl;
}
};
//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.
class Reciever{
private:
unsigned int Rx_Pkt;
unsigned int Rx_Data;
unsigned int E_Flag;
public:
Reciever(){}
Reciever(unsigned int Rx_Pkt){
this->Rx_Pkt=Rx_Pkt;
}
void get_Rx_Pkt()
{
cout<<"Enter Rx_Pkt: ";
cin>>Rx_Pkt;
}
void recievePacket()
{
unsigned int result = Rx_Pkt ^ (Rx_Pkt >> 1);
result = result ^ (result >> 2);
result = result ^ (result >> 4);
result = result ^ (result >> 8);
result = result ^ (result >> 16);
if (result & 1){
E_Flag=1;
}else{
E_Flag=0;
}
if(E_Flag == 0)
{
Rx_Data = Rx_Pkt-32768;
cout<<"Rx_Data: "<<Rx_Data<<"\n";
}
else{
cout<<"The packet is corrupted\n";
}
}
};
//Derive a class called Tranceiver from the above two
//classes and check the functionalities. Use any other functions if needed.
class Tranciever: public Transmitter, public Reciever{
public:
void options(){
int choice=0;
cout<<"1. Calculate parity bit\n";
cout<<"2. Calculate parity bit\n";
cout<<"Your choice: ";
cin>>choice;
if(choice==1){
get_pay_load();
parity_bit();
dataPacket();
}
if(choice==2){
get_Rx_Pkt();
recievePacket();
}
}
};
int main(){
Tranciever tranciever;
tranciever.options();
system("pause");
return 0;
}
Comments
Leave a comment