C++ Example Program of Modules

All are extracted from CS 210 assignments
 
program1



//maindemo.cpp

#include 
#include "square.h"
#include "linklist.h"
#include "linklist.cpp"
main()
{
	// try executes the codes between the {} and if there is an
	// exception, it is handled by the following catch statements
	try
	{
		// 1. Create a new linkedlist at runtime
		LinkedList *mylist = new LinkedList();

		// create several squares of different sizes
		square sq1(1);
		square sq2(2);
		square sq3(3);
		square sq4(4);
		square sq5(5);		
		square sq6(6);        
		square sq7(7);		
		square sq8(8);

		// 2. Add several objects to the list

		mylist->Append(sq1);
		mylist->Prepend(sq2);
		mylist->Append(sq3);
		mylist->Prepend(sq4);		
		mylist->Extract(sq1);				

		// Display the list
		mylist->DisplayList();
		// 3. Delete the list
		delete mylist;

	}

	// This catches invalid argument exceptions and outputs 
	// an error message.
	catch (argument_error eObj)
	{
		cout << eObj.what() << endl; } catch (domain_error eObj) { cout << eObj.what() << endl; } return 0; } program2


//   exception.cpp

#if !defined (exception_h)
    #define exception_h

    #include 
    #include 
    #include 
    #include "strng.h"

    class logic_error : public exception
    {
    public:
	logic_error (string const& s) : exception (s.data ()) {}
    };

    class domain_error : public logic_error
    {
    public:
	domain_error (string const& s) : logic_error (s) {}
    };

    class out_of_range : public logic_error
    {
    public:
	out_of_range (string const& s) : logic_error (s) {}
    };

    class argument_error : public logic_error
    {
    public:
	argument_error (string const& s) : logic_error (s) {}
    };

    class bad_cast : public exception
    {
    public:
	bad_cast (string const& s) : exception (s.data ()) {}
    };	 
    //   #define bad_alloc xalloc
    class bad_alloc : public exception    
    {    public:	
                bad_alloc (string const& s) : exception (s.data ()) {}    
    };


#endif


program3



//linklist.cpp
#if !defined (linklist_c)
    #define linklist_c

    #include 
    #include "exception.h"
    #include "linklist.h"

template 
ListElement::ListElement (
    T const& _datum, ListElement* _next) :
    datum (_datum), next (_next)
    {}

template 
T const& ListElement::Datum () const
    { return datum; }

template 
ListElement const* ListElement::Next () const
    { return next; }

template 
void ListElement::DisplayElement() const
{
	cout << "[" << datum << "]"; if (next !="0)" { cout << "-->";
	}
}

template 
LinkedList::LinkedList () :
    head (0),
    tail (0)
    {}

template 
void LinkedList::Purge ()
{
    while (head != 0)
    {
	ListElement* const tmp = head;
	head = head->next;
	delete tmp;
    }
    tail = 0;
}

template 
LinkedList::~LinkedList ()
    { Purge (); }

template 
ListElement const* LinkedList::Head () const
    { return head; }

template 
ListElement const* LinkedList::Tail () const
    { return tail; }

template 
bool LinkedList::IsEmpty () const
    { return head == 0; }

template 
T const& LinkedList::First () const
{
    if (head == 0)
	throw domain_error ("list is empty");
    return head->datum;
}

template 
T const& LinkedList::Last () const
{
    if (tail == 0)
	throw domain_error ("list is empty");
    return tail->datum;
}

template 
void LinkedList::Prepend (T const& item)
{
    ListElement* const tmp = new ListElement (item, head);
    if (head == 0)
		tail = tmp;
    head = tmp;
}

template 
void LinkedList::Append (T const& item)
{
    ListElement* const tmp = new ListElement (item, 0);
    if (head == 0)
		head = tmp;
    else
		tail->next = tmp;
    tail = tmp;
}

template 
LinkedList::LinkedList (LinkedList const& linkedList) :
    head (0),
    tail (0)
{
    ListElement const* ptr;
    for (ptr = linkedList.head; ptr != 0; ptr = ptr->next)
		Append (ptr->datum);
}

template 
LinkedList& LinkedList::operator = 
                             (LinkedList const& linkedList)
{
    if (&linkedList != this)
    {
	Purge ();
	ListElement const* ptr;
	for (ptr = linkedList.head; ptr != 0; ptr = ptr->next)
		Append (ptr->datum);
    }
    return *this;
}

template void LinkedList::Extract (T const& item)
{    
	ListElement* ptr = head;    
	ListElement* prevPtr = 0;    
	while (ptr != 0 && ptr->datum != (T) item)    
	{		
		prevPtr = ptr;		
		ptr = ptr->next;    
	}    
	if (ptr == 0)		
		throw argument_error ("item not found");    
	if (ptr == head)		
		head = ptr->next;    
	else		
		prevPtr->next = ptr->next;    
	if (ptr == tail)		
		tail = prevPtr;    
	delete ptr;	
}
template 
void LinkedList::InsertAfter (ListElement const* arg, T const& item)
{
    ListElement* ptr = (ListElement*) arg;
    if (ptr == 0)
		throw argument_error ("invalid position");
    ListElement* const tmp =
		new ListElement (item, ptr->next);
    ptr->next = tmp;
    if (tail == ptr)
		tail = tmp;
}

template 
void LinkedList::InsertBefore (ListElement const* arg, T const& item)
{
    ListElement* ptr = (ListElement*) arg;
    if (ptr == 0)
		throw argument_error ("invalid position");
    ListElement* const tmp = new ListElement (item, ptr);
    if (head == ptr)
		head = tmp;
    else
    {
		ListElement* prevPtr = head;
		while (prevPtr != 0 && prevPtr->next != ptr)
			prevPtr = prevPtr->next;
		if (prevPtr == 0)
			throw argument_error ("invalid position");
		prevPtr->next = tmp;
    }
}


template 
void LinkedList::DisplayList() const
{
	ListElement *tmp = (ListElement *) head;
	while (tmp != 0)
	{
		tmp->DisplayElement();
		tmp = tmp->next;
	}
	cout << endl; } template < class T>int LinkedList::Size ()
{   
	ListElement* ptr = head;   
	int count=0;    
	while (ptr != 0)   
	{		
		ptr = ptr->next;		
		count++;   
	}    
	return count;   
}


template 
T const& LinkedList::FindMax() const 
{		
	ListElement const* ptr=(ListElement const*) head;	
	ListElement const* max=(ListElement const*) head;		
	if (IsEmpty())		
		throw domain_error("list is empty");	
	else	
	{    	
	while(ptr!=0)		
		{		
		if(max->datumdatum)		   
			max=ptr;		
		ptr=ptr->next;		
		}	
	}	
	return max->datum;	
}


template ListElement 
const*LinkedList::Find (T const& value) const
{    
	ListElementconst* ptr = head;	
	if (ptr == 0)		
		throw argument_error ("item not found");        
	while (ptr!=0)     
	{				
	if(ptr->Datum() == value)		
		break;		
	ptr=ptr->Next();	
	}        
	return ptr;
}
#endif




program4

//linklist.h
#if !defined (linklist_h)
    #define linklist_h

    #include 

template 
class LinkedList;

template 
class ListElement
{
    T datum;
    ListElement* next;

    ListElement (T const&, ListElement*);
public:
    T const& Datum () const;
    ListElement const* Next () const;
	void DisplayElement() const;

    friend LinkedList;
};

template 
class LinkedList
{
    ListElement* head;
    ListElement* tail;
public:
    LinkedList ();
    ~LinkedList ();

    LinkedList (LinkedList const&);
    LinkedList& operator = (LinkedList const&);

    ListElement const* Head () const;
    ListElement const* Tail () const;	ListElement const* Find(T const&) const;
    bool IsEmpty () const;
    T const& First () const;
    T const& Last () const;
	T const& FindMax() const;
    void Prepend (T const&);
    void Append (T const&);
    void Extract (T const&);    void Purge ();
    void InsertAfter (ListElement const*, T const&);
    void InsertBefore (ListElement const*, T const&);	void DisplayList() const;		int Size();
};

//    #include "linklist.cpp"

#endif


program5

//  string.cpp

#include 
#include  
#include 
#include 
#include 
#include "strng.h"

string::string () :
    length (1),
    array (new char [1])
{
    assert (array != 0);
    array [0] = 0;
}

string::string (string const& string) :
    length (string.length),
    array (new char [string.length])
{
    assert (array != 0);
    strncpy (array, string.array, length);
}

string::string (char const* s) :
    length (strlen (s) + 1),
    array (0)
{
    array = new char [length];
    assert (array != 0);
    strncpy (array, s, length);
}

string::string (char c) :
    length (2),
    array (new char [2])
{
    assert (array != 0);
    array [0] = c;
    array [1] = 0;
}

string::~string ()
    { delete [] array; }

string& string::operator = (char c)
{
    delete [] array;
    length = 2;
    array = new char [length];
    assert (array != 0);
    array [0] = c;
    array [1] = 0;
    return *this;
}

string& string::operator = (string const& string)
{
    if (this != &string)
    {
		delete [] array;
		length = string.length;
		array = new char [length];
		assert (array != 0);
		strncpy (array, string.array, length);
    }
    return *this;
}

string& string::operator = (char const* s)
{
    if (array != s)
    {
		delete [] array;
		length = strlen (s) + 1;
		array = new char [length];
		assert (array != 0);
		strncpy (array, s, length);
    }
    return *this;
}

string& string::append (string const& string)
{
    unsigned int newLength = length + string.length - 1;
    char* newData = new char [newLength];
    strncpy (newData, array, length);
    strncat (newData, string.array, string.length);
    delete [] array;
    array = newData;
    length = newLength;
    return *this;
}

char string::operator [] (unsigned int pos) const
{
    assert (pos <= length); return array [pos]; } int string::compare (string const& string) const { return strncmp (array, string.array, length); } char const* string::data () const { return array; } void string::Put (ostream& s) const { s << array; } unsigned short int const bufferSize="256;" void string::Get (istream& s) { char buffer [bufferSize]; s>> setw (bufferSize) >> buffer;
    delete [] array;
    length = strlen (buffer) + 1;
    array = new char [length];
    assert (array != 0);
    strncpy (array, buffer, length);
}


program6

//   string.h

#if !defined (strng_h)
    #define strng_h

    #include 
    #include 

    class string
    {
		char* array;
		unsigned int length;
	public:
		string ();
		string (char const*);
		string (string const&);
		string (char);

		~string ();

		char operator [] (unsigned int) const;

		string& operator = (char);
		string& operator = (char const*);
		string& operator = (string const&);

		string& append (string const&);

		int compare (char const*) const;
		int compare (string const&) const;

		char const* data () const;

		void Put (ostream&) const;
		void Get (istream&);
    };

    inline ostream& operator << (ostream& s, string const& strng) { strng.Put (s); return s; } inline istream& operator>> (istream& s, string& strng)
		{ strng.Get (s); return s; }

#endif



Code Reference:
CS 210 Assignments.


Author: Quanxing Li 
Last Modified on June 8, 2000
 Copyright 2000 Department of Computer Science, University of Regina. 


Previous
Index

[CS Dept Home Page]