Hello everyone. I am trying to create a circular queue and need some pointers. My questions are as follows:
1. Will my queue class produce a circular queue?
2. The random contributors are not being added correctly to the queue. When I dequeue() the queue, it thinks it carries default values assigned from my constructor.
All help is greatly appreciated.
Here is the code:
main.cpp:
queue.h:Code:#include <iostream> #include <ctime> #include <cstdlib> using namespace std; #include "contributor.h" #include "queue.h" int main() { MyQueue Q; double Con,ID; short Sex; srand(time(0)); for(int j = 0; j < Q.getSize(); j++) { Con = rand() % 1000 + 1; Sex = rand() % 10 + 1; ID = rand() % 100 + 1; if (Sex > 4) Q.Enqueue(Contributor("Random Male",male,Con,ID)); else Q.Enqueue(Contributor("Random Female",female,Con,ID)); cout << Q.Dequeue() << endl; } cout << "Size = " << Q.getSize() << endl; cout << Q.ShowFront() << endl; return 0; }
queue.h:Code:#ifndef QUEUE_H #define QUEUE_H #include "contributor.h" class MyQueue { public: MyQueue(); ~MyQueue(){delete [] mArray;} void Enqueue(Contributor &); const Contributor & Dequeue(); const Contributor & ShowFront() const{return mArray[mFront];} bool Is_Empty() const{return mFront == mRear;} bool Is_Full() const{return mRear + 1 % MaxSize == mFront;} short getSize() const{return MaxSize;} private: int mSize,mFront,mRear; short MaxSize; Contributor *mArray; }; #endif
contributor.h:Code:#include <iostream> #include <cstdlib> #include <ctime> #include <stack> using namespace std; #include "queue.h" #include "contributor.h" MyQueue::MyQueue() { MaxSize = 25; mArray = new Contributor[MaxSize]; mRear = mFront = mSize = 0; } void MyQueue::Enqueue(Contributor &cont) { if (!Is_Full()) { mArray[mRear] = cont; mRear = (mRear + 1) % MaxSize; mSize++; } else cout << "ERROR: could not add to the queue - queue is full." << endl; } const Contributor & MyQueue::Dequeue() { if (!Is_Empty()) { mFront = (mFront + 1) % MaxSize; return mArray[mFront]; mSize--; } else { cout << "ERROR: could not remove from the queue - queue is empty." << endl; exit(1); } }
contributor.cpp:Code:#ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender_type {male,female,unknown}; class Contributor { public: Contributor(); Contributor(string,Gender_type,double,int); Contributor(const Contributor &); ~Contributor (); friend ostream & operator<< (ostream &, const Contributor &); friend istream & operator>> (istream &, Contributor &); const Contributor & operator= (const Contributor &); string getName(){return Name;} double getContribution(){return Contribution;} int getIDKey(){return IDKey;} Gender_type getGender(){return Gender;} short getGender1(); bool operator< (const Contributor &) const; bool operator> (const Contributor &) const; bool operator>= (const Contributor &) const; bool operator== (const Contributor &) const; bool operator!= (const Contributor &) const; private: string Name; double Contribution; int IDKey; Gender_type Gender; }; #endif
Code:#include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; #include "queue.h" #include "contributor.h" Contributor::Contributor() { Contribution = 0.00; Name = "Blank"; Gender = unknown; IDKey = 0; } Contributor::Contributor(string name,Gender_type g,double contribution,int idkey) { Name = name; Gender = g; Contribution = contribution; IDKey = idkey; } Contributor::Contributor(const Contributor &c) { Name = c.Name; Gender = c.Gender; Contribution = c.Contribution; IDKey = c.IDKey; } Contributor::~Contributor () {} ostream & operator<< (ostream &out, const Contributor &c) { string temp; if (c.Gender == male) temp = "male "; if (c.Gender == female) temp = "female"; if (c.Gender == unknown) temp = "unknown"; out << "Name = " << c.Name << "\tGender = " << temp << "\t" << "Contribution = " << "$" << c.Contribution << ".00" << "\t" << "IDKey = " << c.IDKey; return out; } istream & operator>> (istream &in, Contributor &r) { string temp; cout << "Enter a name (no spaces): "; in >> r.Name; cout << "Enter gender (male, female, unknown): "; in >> temp; if (temp == "male") r.Gender = male; if (temp == "female") r.Gender = female; if (temp == "unknown") r.Gender = unknown; if (temp != "unknown" && temp != "male" && temp != "female") r.Gender = unknown; cout << "Enter a contribution: "; in >> r.Contribution; cout << "Enter an ID Key: "; in >> r.IDKey; return in; } const Contributor & Contributor::operator= (const Contributor &rhs) { Contribution = rhs.Contribution; Name = rhs.Name; Gender = rhs.Gender; IDKey = rhs.IDKey; return *this; } bool Contributor::operator< (const Contributor &rhs) const { return (Contribution < rhs.Contribution); } bool Contributor::operator> (const Contributor &rhs) const { return (Contribution > rhs.Contribution); } bool Contributor::operator>= (const Contributor &rhs) const { return (Contribution >= rhs.Contribution); } bool Contributor::operator== (const Contributor &rhs) const { return (Name == rhs.Name); } bool Contributor::operator!= (const Contributor &rhs) const { return (Name != rhs.Name); } short Contributor::getGender1() { if (getGender() == male) return 0; if (getGender() == female) return 1; else return 2; }



LinkBack URL
About LinkBacks


