![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Well, what about my questions?
![]() |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Quote:
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? |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|