Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Home Library Index Site Map  Links Search About

A functional Vector Container

This code is in an interesting intermediate stage.
Following some excellent advice in Bjarne Stroustrup's book The C++ Programming language I approach templates in the following way;
Rather than having to deal with abstractions throughout the development process, I use a concrete built-in class, as similar as possible to the final intended template parameter and develop the functionality.

That is the current stage of this project.

All that is now required, is to change instances of data_type in the code to T and alter the declaration to template syntax.  

 

Vector
// INCLUDES
#include <iostream.h>

// NAME: Vec.H - TYPE: C++ CLASS TEMPLATE (keep it simple!)
//
// SYNOPSIS:
// DESCRIPTION: Interface and implementation
// for the Vec template class
// EXAMPLES:
// BUGS:
// SEE ALSO:
////////////////////////////////////////////

enum Exceptions {EOutOfBounds, EOutOfMemory, EDivByZero, EDiskAccess, ESelfAssign};

typedef int data_type;

class Vec
{
public:
// LIFECYCLE

Vec(int n); // default constructor
Vec(const Vec&); // copy constructor
virtual ~Vec() {delete [] data;} // destructor

// OPERATORS
const Vec& operator=( const Vec&); // assignment operator
data_type operator[](int i) {return data[i];}
// OPERATIONS
void Init();
void Show();
// ACCESS
// INQUIRY
data_type *data;

protected:

private:

int sz, // size of array
lb, // lower bound
hb // higher bound
;
void at(int i);

};

// METHODS
//

Vec::Vec(int n) : sz(n), lb(0), hb(10)
{
at(n);
data = new data_type[n];
if(!data)
throw EOutOfMemory;
Init();

};

Vec::Vec(const Vec& v) // copy constructor
{
this->sz =v.sz;
this->lb =v.lb;
this->hb =v.hb;
data = new data_type[sz];
for(int i=0; i<sz; i++)
data[i] =v.data[i];

};


void
Vec::Init()
{
for(int i=0; i<sz; i++)
data[i] =i;
};

void
Vec::Show()
{
for(int i=0; i<sz; i++)
cout<<data[i];
};

void
Vec::at(int i)
{
if(i<lb || i>hb)
throw EOutOfBounds;

};


const Vec&
Vec::operator=(const Vec& v)
{
if(this == &v)
throw ESelfAssign;

for(int i=0; i<sz; i++)
data[i] =v.data[i];

return *this;

};

Note that I have used a typedef data_type to represent int.
This is to ensure it is a simple matter to replace the correct instances of int in the code when converting to a template

Home Library Index Top

 

Vector
Driver
int main()
{
Exceptions except;

try{
Vec inst(10), inst2(10);
for(int i =0;i<10;i++)
inst.data[i] =0;

cout<<"inst =";
inst.Show();
cout<<endl;
cout<<"inst2 =";
inst2.Show();
cout<<"\nAfter assignment : ";
cout<<endl;
inst2 =inst;
cout<<"inst2 =";
inst2.Show();
cout<<endl;
cout<<"\nAfter copying : ";
cout<<endl;
Vec inst3(inst);
cout<<"inst3 =";
inst.Show();
}//try

catch(Exceptions e)
{
switch(e)
{
case EOutOfBounds : cerr<<"\n\nRange out of bounds"<<endl;
break;
case ESelfAssign : cerr<<"\n\nSelf Assignment Error"<<endl;
break;

default : cerr<<"Unhandled Exception";
}//switch

}// catch

catch(...)
{
cerr<<"Unknown Exception";
};

return 0;
}
Note:
Home Library Index Top

 

STL Database
Arrays
Binary File Input / Output
Character Input
Containers
Standard Template Library
Streams
Templates
Utility Functions
Win32 Programming
Miscellaneous
 

 
copyright notice

Copyright Robert Mitchell. Last Revised : 28 April, 2000

e-mail me
1