1. Please use c++ for the following question. 2. PLEASE SHOW ALL OUTPUT. 3. Writ
ID: 3821607 • Letter: 1
Question
1. Please use c++ for the following question.
2. PLEASE SHOW ALL OUTPUT.
3. Write CppUnitLite tests to verify class functionality.
4. Define a class CharQueue with the public interface:
Copy the public interface above into two different versions of CharQueue:
CharQueue1 with a char* array as the private data member. Use new to grow the array used for the queue as needed. Add other private member variables as needed to track size and position. Overload the copy constructor and assignment operator to and implement a deep copy.
CharQueue2 with a std::deque<char> data member. Use the compiler generated copy constructor and assignment operator as a bitwise copy of the deque will perform a deep copy.
Place each class in two separate files named for the class. For example, for CharQueue1 place the class declaration in CharQueue1.h. Place the class definition in CharQueue1cpp.
Explanation / Answer
#include <iostream>
#include <string.h>
using namespace std;
class CharQueue1
{
public:
public:
char* q;
int size;
int cap;
CharQueue1();
CharQueue1(size_t size);
CharQueue1(const CharQueue1& src); // copy constructor
void enqueue(char ch);
char dequeue();
bool isEmpty() const;
void swap(CharQueue1& src);
int capacity() const;
CharQueue1& operator=(CharQueue1 src);
void print();
};
CharQueue1::CharQueue1(){
this->size =0;
q = new char[10]; // default capacity =10
cap=10;
}
CharQueue1::CharQueue1(size_t size){
this->size = 0;
cap = size;
q = new char[cap];
}
CharQueue1::CharQueue1(const CharQueue1& src){
size = src.size;
q = new char[size];
strcpy(q,src.q);
}
void CharQueue1::enqueue(char ch){
if(size>=cap){
char* temp = q;
q= new char[2*size];
cap = 2*size;
int i;
for(i=0;i<size;i++){
q[i] = temp[i];
}
}
q[size] = ch;
size++;
}
char CharQueue1::dequeue(){
char ch =-1;
if(size>0){
ch = q[0];
int i;
for(i=0;i<size-1;i++){
q[i] = q[i+1];
}
size--;
}
return ch;
}
bool CharQueue1::isEmpty()const{
if(size>0)return false;
else true;
}
int CharQueue1::capacity() const{
return cap;
}
void CharQueue1::swap(CharQueue1& src){
char* tempq = q;
int s = size;
int c = cap;
q = src.q;
src.q = tempq;
cap = src.cap;
src.cap = c;
size = src.size;
src.size = s;
}
CharQueue1& CharQueue1::operator=(CharQueue1 src){
CharQueue1& queue(src);
return queue;
}
void CharQueue1::print(){
int i;
cout<<" [ ";
for(i=0;i<size;i++){
cout<<q[i]<<" ";
}
cout<<" ] ";
}
int main()
{
CharQueue1 q(10);
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.enqueue('d');
q.print();
char ch = q.dequeue();
q.print();
cout << "dequeue : " <<ch<< endl;
cout << "size : " <<q.size<< endl;
ch = q.dequeue();
cout << "dequeue : " <<ch<< endl;
cout << "size : " <<q.size<< endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.