Project 2(C++) Due: 10 March 2018 by 11:55 PM Progam needs to be in C++ In Proje
ID: 3722325 • Letter: P
Question
Project 2(C++) Due: 10 March 2018 by 11:55 PM
Progam needs to be in C++
In Project 1 we laid the foundation of what will become a simple network packet transmission simulation. This project will build upon this foundation, utilizing a queue structure to store atomic packets of data which will travel across the network. The advantage of a queue is that, once a routing system is constructed in Project 3, latency will naturally be minimized as packets are essentially prioritized by wait time.
Please construct the following class, defined in a file named Packet.h, and define the function bodies in a separate Packet.cpp file:
Packet
This class should contain the following private variables. You should define public functions which get and set the values of these variables.
• An integer called targetID
• An integer called sourceID
• A string called data
Network Object
This class (from Project 1) should be modified to contain a private queue variable called packets. I'm trusting that you will recall the form of a FIFO queue from CSC 220 Java Data Structures. For those who do not recall, the queue will be an easy subject to Google and learn about.
How you decide to implement the queue is up to you, so long as it functions according to the normal FIFO expectations of a queue and stores packet objects. More accurately, you will find that storing pointers to packet objects will be far superior than the actual packet object. As we discussed in class, the power of the pointer is in its ability to reference large objects in a compact, easily accessed way
We will use this queue of packets in Project 3 to begin routing packets between network objects and begin to simulate communication between network objects.
(Project 1 Q&A given below, already done, Proj 3 is not given to us yet)
Project 1(This was the question for project 1 and the code that i submitted is below this)
Due: February 14, 2018 at 11:55 PM
This project lays the foundation for a series of programs which address various problems in computer networking. Before diving into the complex problems, it is important to build experience with the programming language and techniques we will use to solve them. As demonstrated in class, the syntax of C++ is very similar to Java, so establishing a level of comfort while exercising problem-solving skills is emphasized here rather than minutiae of the language. Those details will be incorporated into later projects through application. Keep in mind that there are many resources to learn the syntactic details of C++. The required and recommended textbooks listed in the Syllabus and the class slides are good resources. Using the internet to find additional resources is encouraged, however the policy on plagiarism is clear and very strict. Do not copy and submit code which is not your own.
Problem Description
This phase of the project will establish two classes. Their specific implementation details are flexible, but they should meet the following requirements:
NetworkObject
This class called NetworkObject defines the abstract properties of an element of our network. Additional capabilities will be added to classes derived from this base class in order to power future projects.
The class should contain at least the following private variables:
int objectId
The class should contain at least the following public methods:
NetworkObject(int objectId)
Constructor which sets the objectId when an object is created.
int getObjectId()
Returns the objectId variable
Server
This class is a “dummy” derived class to which we will add complexity in future projects. The class should inherit from the NetworkObject class (we will go into detail about inheritance in the 2/10 lecture). For now, it requires no additional functionality beyond a constructor method.
Other Requirements
In your main function, choose a way to store a number of NetworkObject objects. This may be an array, a vector, a linked list, etc, it is your design choice at this stage. The main function should then instantiate some objects and then print their objectId’s using the functions and classes created above.
The code i used for Project 1
#include<iostream>
using namespace std;
class NetworkObject
//beginning Network Object class definition
{
public:
//Constructor
NetworkObject(int objId): objectId(objId){
}
//Parameterized constructor
NetworkObject();
//returns objectId
int getObjectId(){
return objectId;
}
private:
//private member
int objectId;
};
//server class constructor is called from networkobject class parameterized constructor
class Server : public NetworkObject
{
public:
Server(int objId): NetworkObject(objId){}
};
int main()
{
//declare 6 NetworkObjects with id 4,8,16, 32, 64, 128.
NetworkObject at[6] = {{4}, {8}, {16}, {32}, {64}, {128}};
for(int i=0; i<6;i++)
{
//prints the object IDs of the network objects
cout << "Object ID of Network Object: "<< at[i].getObjectId() << endl;
}
}
Explanation / Answer
Main
#include<iostream>
#include<queue>
#include "Packet.h"
using namespace std;
class NetworkObject
//beginning Network Object class definition
{
public:
//Constructor
NetworkObject(int objId): packets(),objectId(objId){
}
//Parameterized constructor
NetworkObject();
//push packet into the queue that belong to one particular NetworkObject
void setPacket(Packet *pckt)
{
packets.push(pckt);
}
//pop packet one by one that belongs one particular NetworkObject
Packet *popPacket()
{
Packet *pkt;
pkt = packets.front();
packets.pop();
return pkt;
}
//retrieve all the packets of a particular NetworkObject
void getPacketInfo()
{
Packet *pkt;
while(!packets.empty())
{
pkt = popPacket();
cout <<" Packets from source "<<pkt->getSourceID()<< " to destination "<<pkt->getTargetID() <<" with data "<<pkt->getData();
}
}
//returns objectId
int getObjectId(){
return objectId;
}
private:
//private member
int objectId;
queue<Packet*> packets; //packets variable to store the packets in an queue
};
//server class constructor is called from networkobject class parameterized constructor
class Server : public NetworkObject
{
public:
Server(int objId): NetworkObject(objId){}
};
int main()
{
//declare 6 NetworkObjects with id 4,8,16, 32, 64, 128.
NetworkObject at[6] = {{4}, {8}, {16}, {32}, {64}, {128}};
//Instantiate two objects of type Packet
Packet *pckt1 = new Packet(8,4,"8 to 4");
Packet *pckt2 = new Packet(8,5,"8 to 5");
//push the above created packets in the queue
at[1].setPacket(pckt1);
at[1].setPacket(pckt2);
for(int i=0; i<6;i++)
{
//prints the object IDs of the network objects along with packet information
cout << " Object ID of Network Object: "<< at[i].getObjectId() << endl;
at[i].getPacketInfo();
}
}
-----------------------------
Packet.h
#include <string>
using namespace std;
class Packet
{
public:
//constructor
Packet(int srcId, int trgId, string dt);
// set method for sourceid
void setSourceID(int srcID);
// get method for sourceid
int getSourceID();
// set method for targetid
void setTargetID(int trgID);
// get method for targetid
int getTargetID();
// set method for data
void setData(string dt);
// get method for data
string getData();
private:
int targetID;
int sourceID;
string data;
};
------------------------------
Packet.cpp
#include "Packet.h"
//constructor to initialize member variables
Packet::Packet(int srcId, int trgId, string dt)
{
sourceID = srcId;
targetID = trgId;
data = dt;
}
//set method for sourceID
void Packet::setSourceID(int srcID)
{
sourceID = srcID;
}
//get method for sourceID
int Packet::getSourceID()
{
return sourceID;
}
//set method for targetID
void Packet::setTargetID(int trgID)
{
targetID = trgID;
}
//get method for targetID
int Packet::getTargetID()
{
return targetID;
}
//set method for data
void Packet::setData(string dt)
{
data = dt;
}
//get method for data
string Packet::getData()
{
return data;
}
-----------------
Output
Object ID of Network Object: 4
Object ID of Network Object: 8
Packets from source 8 to destination 4 with data 8 to 4
Packets from source 8 to destination 5 with data 8 to 5
Object ID of Network Object: 16
Object ID of Network Object: 32
Object ID of Network Object: 64
Object ID of Network Object: 128
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.