Question: Implement CandidateQueue.cpp MUST BE IN C++ source.cpp file /* In this program, we have a line of candidates, some are developer and some are designer.
Implement CandidateQueue.cpp
MUST BE IN C++
source.cpp file
/* In this program, we have a line of candidates, some are developer and some are designer. When we want to create a team, we need one deisgner and one developer. The candidates are all standing in the line. */ #include#include "CandidateQueue.h" using namespace std; int main() { // Variables used for testing Candidate daniela("Daniela", "Developer"); Candidate hector("Hector", "Salesman"); Candidate berta("Berta", "Designer"); Candidate hugo("Hugo", "Salesman"); Candidate bernardo("Bernardo", "Designer"); Candidate daria("Daria", "Developer"); CandidateQueue q; q.push_cadidate(&daniela); q.push_cadidate(&hector); q.push_cadidate(&berta); q.push_cadidate(&hugo); q.push_cadidate(&bernardo); q.push_cadidate(&daria); /* The output should be There is a salesman in the line. First one called Hector There is a designer in the line. First one called Berta There is no one with marketing specialty *********** */ string c = q.Exist("Salesman"); if (c == "") cout << "There is no one with salesman specialty "; else cout << "There is a salesman in the line. First one called " << c << endl; c = q.Exist("Designer"); if (c == "") cout << "There is no one with designer specialty "; else cout << "There is a designer in the line. First one called " << c << endl; c = q.Exist("Marketing"); if (c == "") cout << "There is no one with marketing specialty "< CandidateQueue.h FILE
#pragma once #include "Candidate.h" class CandidateQueue { public: class Node { public: Candidate* c; Node* next; }; // Constructor: initialize head, tail and count CandidateQueue(); // Pushes a pointer to a candidate onto the back of the line (the info of Node is a pointer to Candidate :D ). // update head, tail and count void push_cadidate(Candidate *p); // Check the line and see if there is any candidate with the give role in the line (the line is implemented in linked list) // If there is, return the name of that candidate // If no such candidate exists, returns "". string Exist(string role); private: Node* head; Node* tail; int count; };CandidateQueue.cpp file
#include "CandidateQueue.h"
#include
using namespace std;
CandidateQueue::CandidateQueue()
{
count = 0;
head = nullptr;
tail = nullptr;
}
void CandidateQueue::push_cadidate(Candidate* p) {
//To DO
}
string CandidateQueue::Exist(string r) {
//To DO
}
Candidate.h
#pragma once #includeusing namespace std; class Candidate { public: // Initializes a player with the given name and role. role is either developer or designer Candidate(string name, string role); // Returns the name of the player string name(); // Returns the role of the player string role(); private: string _name; string _role; }; Candidate.cpp #include "Candidate.h" Candidate::Candidate(string name, string role) { _name = name; _role = role; } string Candidate::name() { return _name; } string Candidate::role() { return _role; }The below is the incorrect answer and is not what I want
Answer :
Given below is the completed CandidateQueue.cpp file.
#include "CandidateQueue.h" #include
using namespace std;
CandidateQueue::CandidateQueue() {
count = 0; capacity = 2; candidates = new Candidate*[capacity]; }
void CandidateQueue::push_player(Candidate* p) { if(count == capacity){ capacity *= 2; Candidate **temp = new Candidate*[capacity]; for(int i =0 ;i < count; i++) temp[i] = candidates[i]; delete []candidates; candidates = temp; }
candidates[count++] = p; } Candidate* CandidateQueue::front_player(string r) { for(int i = 0; i < count; i++){ if(candidates[i]->role() == r) return candidates[i]; }
return nullptr;
} void CandidateQueue::pop_player(string r) { int index = -1; for(int i = 0; i < count; i++){ if(candidates[i]->role() == r) { index = i; break; } }
if(index == -1) //not found return;
//move all others one position to left for(int i = index + 1; i < count; i++) candidates[i-1] = candidates[i]; }
Candidate** CandidateQueue::form_group() { Candidate** group = new Candidate*[2]; group[0] = group[1] = nullptr; Candidate *developer, *designer; developer = front_player("Developer"); designer = front_player("Designer"); if(developer != nullptr && designer != nullptr){ group[0] = developer; group[1] = designer; pop_player("Developer"); pop_player("Designer"); } return group; }
CandidateQueue() was not suppose to be messed with as specified in the quesiton! This is a solved problem to another question
You have 2 functions to implement the void CandidateQueue::push_cadidate(Candidate* p) { //To DO } string CandidateQueue::Exist(string r) { //To DO }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
