Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 17th, 2005, 5:11 PM   #1
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
IntArray Class

Hello Everyone!

I made an array class, well I am confused about a few things:

* I've read memset fucntion in a tutorial, I used it to initialize the whole array at once is it ok?

* I want to add a check boundary, like the arrayclass and member fucntion should not accept any number greater than or lesser than index. Where should I add it, and how would I add it?

* Can I convert this array class to accept all kinds of data in one variable? How can I do this?


class IntArray{
	
	public:
	  	 int  addAllElements(void);
		 int  sumOfElements(const int, const int);
		 void defineElement(const int);
		 void initializeAll(void);
		 void initializeAllBy(const int);
		 void showElement(const int);	
		 void readElement(const int);
		 void sortElements(bool);
		 void showElement(const int I, char ch);

	private:
		 int	  size;
 		 int	  length;
		 int	  Element;
		 int	  *Array;
		 bool	  order;
};

void IntArray :: defineElement(const int I){

	length = abs(I);
	Array  = new int[length];
}

void IntArray :: initializeAll(void){

	memset(Array,0,(sizeof Array)*length);
	
}

void IntArray :: initializeAllBy(const int I){

	for(int z=0; z<length; z++)
		*(Array + z) = I;
}

void IntArray :: showElement(const int I){

	Element = abs(I);
	cout << *(Array + Element)
		 << endl;

}	

void IntArray :: showElement(const int I, char ch){

	Element = abs(I);
	cout << *(Array + Element)
		 << ch;

}

void IntArray :: readElement(const int I){
	
	Element = abs(I);
	cin >> *(Array + Element);

}

void IntArray :: sortElements(bool I){
	
	int swaper;
	Element = Array[0];

	if(I == 0) {
		for(int z=0; z<length; z++)
		{
			for(int y=0; y<length-1; y++)
			{
				if( Array[y] > Array [y+1] )
				{
					swaper     = Array[y];
					Array[y]   = Array[y+1];
					Array[y+1] = swaper;

				}

			}

		}
	
	}

	if(I == 1) {
		for(int z=0; z<length; z++)
		{
			for(int y=0; y<length-1; y++)
			{
				if( Array[y] < Array [y+1] )
				{
					swaper     = Array[y];
					Array[y]   = Array[y+1];
					Array[y+1] = swaper;

				}

			}

		}
	
	}

	else if( I != 1 && I != 0 )
	{
		cout << "Please enter 0 for ascending"
			    "or 1 for descending order sorintg";
	}


}

int IntArray :: addAllElements(void){

	for(int sum=0,z=0; z<length; z++)
		sum += Array[z];
	return (sum);
}

int IntArray :: sumOfElements(const int start, const int end){

	for(int sum=0,z=start; z<end; z++)
		sum += Array[z];
	return (sum);
	

}
Planet_EN is offline   Reply With Quote
Old Sep 17th, 2005, 5:22 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If this isn't just a learning exercise (which would be a Good Thang, don't get me wrong), have you considered the STL and, maybe, templates?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 17th, 2005, 5:34 PM   #3
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Quote:
Originally Posted by DaWei
If this isn't just a learning exercise (which would be a Good Thang, don't get me wrong), have you considered the STL and, maybe, templates?
yes I know about STL and templates and I can use them to make my life alot easier, but I am more interested in making my own classes, and yeah this is for excercise.
Planet_EN is offline   Reply With Quote
Old Sep 17th, 2005, 6:01 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
haha...i got flamed once for suggesting that creating a vector class was a waste of time. more power to ya!!!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 17th, 2005, 6:28 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's a waste of time if you're looking for commercial success instead of knowledge . One picks and chooses .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 17th, 2005, 6:58 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There's a high probability that it'll work fine (sizeof Array--a pointer--on a lot of systems is the same as sizeof int), but it's just serendipitous.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 18th, 2005, 12:51 AM   #7
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Well, what about my questions?
Planet_EN is offline   Reply With Quote
Old Sep 18th, 2005, 1:41 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Planet_EN
Well, what about my questions?
As to your first question, you can use memset to initialise the array for your IntArray.

With your second question, on checking that indices are valid, you would add that check anywhere where you wish to get an element of the array (by index). I would probably create a member named ValidIndex(int index) or something similar, and call that wherever needed (and have that member return false if given an invalid index). An alternate strategy which will encourage users of your class to ensure they employ valid indices, would be a member function named ValidateIndex(int index) which throws an exception when the index is invalid. The choice of such approaches (or using both of them) depends on what you're trying to achieve.

Your third question is unclear. If your goal is to adapt your class so it can hold an array of anything, I'd rename it (eg to Array) and make it into a template class. That way you could have an Array<int> or an Array<double> or an Array<AnyOtherType>. One issue with doing that is that you will no longer be able to use memset() to initialise the array data (as using memset() simply writes data to raw memory, and does not play nicely when that memory actually refers to C++ objects that have constructors, destructors, or [especially] virtual member functions.
grumpy is offline   Reply With Quote
Old Sep 18th, 2005, 5:11 AM   #9
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Quote:
Your third question is unclear. If your goal is to adapt your class so it can hold an array of anything, I'd rename it (eg to Array) and make it into a template class. That way you could have an Array<int> or an Array<double> or an Array<AnyOtherType>. One issue with doing that is that you will no longer be able to use memset() to initialise the array data (as using memset() simply writes data to raw memory, and does not play nicely when that memory actually refers to C++ objects that have constructors, destructors, or [especially] virtual member functions.
Is it possible by overloading? I do not want to use templates. Can you just tell me how to overload it for everytype?
Planet_EN is offline   Reply With Quote
Old Sep 18th, 2005, 6:29 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Planet_EN
Is it possible by overloading? I do not want to use templates. Can you just tell me how to overload it for everytype?
Overloading is simply allowing multiple versions of a function, all with the same name, but with each version of the function taking arguments of different types.

If you want to do overloading for all possible types, the solution is to use a template function. That's one of the fundamental purposes of templates.

Why don't you want to use templates?
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC