一个栈的类:
#ifndef_STACK_H_
#define_STACK_H_
typedefunsignedlongItem;
classStack
{
private:
enum{MAX= 10}; // constant specific to class
Itemitems[MAX];// holds stack items
inttop;// index for top stack item
public:
Stack();
boolisempty()const;
boolisfull()const;
//push() returns false if stack already is full, true otherwise
boolpush(constItem &item); // add item to stack
//pop() returns false if stack already is empty, true otherwise
boolpop(Item& item); // pop top intoitem
};
#endif
实现:
#include"stack.h"
Stack::Stack()// create an empty stack
{
top= 0;
}
boolStack::isempty()const
{
returntop == 0;
}
boolStack::isfull()const
{
returntop == MAX;
}
boolStack::push(constItem &item)
{
if(top <MAX)
{
items[top++]= item;
returntrue;
}
else
returnfalse;
}
boolStack::pop(Item& item)
{
if(top >0)
{
item =items[--top];
returntrue;
}
else
returnfalse;
}
******************************************************
下面是非常重要的string类(面试必考):
//strng2.h -- String class definition
#ifndef_STRNG2_H_
#define_STRNG2_H_
#include<iostream>
usingnamespacestd;
classString
{
private:
char*str; // pointer to string
intlen;// length of string
public:
String(constchar* s); // constructor
String();// defaultconstructor
String(constString &st); //copy constructor
~String();// destructor
intlength()const{returnlen; }
//overloaded operators,copy constructor和‘=’重载,因为应用程序中有copyalue,会出现两次调用free。
String&operator=(constString &st); // Assignment operator
String&operator=(constchar* s); // Assignment operator #2
//friend functions
friendbooloperator>(constString&st1,constString&st2);
friendbooloperator<(constString &st,constString&st2);
friendbooloperator==(constString &st,constString&st2);
friendostream &operator<<(ostream& os,constString &st);
friendistream &operator>>(istream& is,String& st);
String1operator()(String1); //没有对“()”进行重载的话,会出现doublefree的问题!!!
};
#endif
实现:
//strng2.cpp -- String class methods
#include<iostream>
#include<cstring>
usingnamespacestd;
#include"strng2.h"
//class methods
String::String(constchar* s) // make String from C string
{
len=strlen(s);
str=newchar[len+ 1]; // allot storage
strcpy(str,s);// initialize pointer
}
String::String()// default constructor
{
len= 0;
str=newchar[1];
str[0]='\0'; // default string
}
String::String(constString &st)// copy constructor
{
len= st.len;
str=newchar[len+ 1];
strcpy(str,st.str);
}
String::~String()// destructor
{
delete[]str; // required
}
//assign a String to a String
String&String::operator=(constString &st)
{
if(this== &st)
return*this;
delete[]str;
len= st.len;
str=newchar[len+ 1];
strcpy(str,st.str);
return*this;
}
//assign a C string to a String
String&String::operator=(constchar* s)
{
delete[]str;
len=strlen(s);
str=newchar[len+ 1];
strcpy(str,s);
return*this;
}
//true if st1 follows st2 in collating sequence
booloperator>(constString&st1,constString&st2)
{
if(strcmp(st1.str,st2.str) >0)
returntrue;
else
returnfalse;
}
//true if st1 precedes st2 in collating sequence
booloperator<(constString&st1,constString&st2)
{
if(strcmp(st1.str,st2.str) <0)
returntrue;
else
returnfalse;
}
//friends
//true if st1 is the same as st2
booloperator==(constString&st1,constString&st2)
{
if(strcmp(st1.str,st2.str) ==0)
returntrue;
else
returnfalse;
}
//display string
ostream&operator<<(ostream& os,constString &st)
{
os<< st.str;
returnos;
}
//quick and dirty String input
istream&operator>>(istream& is,String& st)
{
chartemp[80];
is.get(temp,80);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
returnis;
}
String1String1::operator()(String1 s)
{
len= s.len;
str= new char[len];
strcpy(str,s.str);
return*this;
}
*****************************************************************************
//queue.h -- interface for a queue
#ifndef_QUEUE_H_
#define_QUEUE_H_
//This queue will contain Customer items
classCustomer
{
private:
longarrive;// arrival time for customer
intprocesstime;// processing time for customer
public:
Customer(){arrive =processtime= 0; }
voidset(longwhen);
longwhen()const{returnarrive; }
intptime()const{returnprocesstime;}
};
typedefCustomerItem;
classQueue
{
//class scope definitions
//Node is a nested structure definition local to this class
structNode { Itemitem;structNode *next;};
enum{Q_SIZE= 10};
private:
Node*front; // pointer to front of Queue
Node*rear; // pointer to rear of Queue
intitems;// current number of items in Queue
constintqsize;// maximum number of items in Queue
//preemptive definitions to prevent public copying
Queue(constQueue &q) : qsize(0){ }
Queue&operator=(constQueue &q) { return*this;}
public:
Queue(intqs = Q_SIZE);// create queue with a qs limit
~Queue();
boolisempty()const;
boolisfull()const;
intqueuecount()const;
boolenqueue(constItem&item);// add item to end
booldequeue(Item&item);// remove item fromfront
};
#endif
实现:
//queue.cpp -- Queue and Customer methods
#include"queue.h"
#include<cstdlib>// (or stdlib.h) for rand()
usingnamespacestd;
//Queue methods
Queue::Queue(intqs) : qsize(qs)
{
front=rear =NULL;
items= 0;
}
Queue::~Queue()
{
Node* temp;
while(front !=NULL) // while queue is not yet empty
{
temp =front;// save address of front item
front=front->next;//reset pointer to next item
deletetemp;// delete former front
}
}
boolQueue::isempty()const
{
returnitems == 0;
}
boolQueue::isfull()const
{
returnitems ==qsize;
}
intQueue::queuecount()const
{
returnitems;
}
//Add item to queue
boolQueue::enqueue(constItem &item)
{
if(isfull())
returnfalse;
Node* add =newNode;// create node
if(add == NULL)
returnfalse;// quit if none available
add->item= item;// set node pointers
add->next= NULL;
items++;
if(front ==NULL) // if queue is empty,
front= add;// place item at front
else
rear->next= add; // else place at rear
rear= add;// have rear point tonew node
returntrue;
}
//Place front item into item variable and remove from queue
boolQueue::dequeue(Item& item)
{
if(front ==NULL)
returnfalse;
item =front->item;// set item to first item in queue
items--;
Node* temp =front; // save location of first item
front=front->next;// reset front to next item
deletetemp;// delete former firstitem
if(items ==0)
rear= NULL;
returntrue;
}
//customer method
//when is the time at which the customer arrives
//the arrival time is set to when and the processing
//time set to a random value in the range 1 - 3
voidCustomer::set(longwhen)
{
processtime= rand() % 3 + 1;
arrive= when;
}