GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Mar-2005, 12:55
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

i need an advice


i am still fresh at using classes.sometimes i get stuck.
i tried to create a class called HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each.then i provided those member functions add,substract,input,output,....etc....some of them i wanted then just boolien types to return true and false.for some reason i feel i missed some things.it would really help if someone can take a quick look at it and tell me where am i going wrong with this.
thanks in advance.
ciao

CPP / C++ / C Code:
//defenition of class HugeInteger
#ifndef HugeInteger_H
#define HugeInteger_H


class HugeInteger {

public :

HugeInteger(int [] = 0);//default constructor

//get function usually declared const
int getarray() const;

void input(int []);
void output(int []);
void add(*[]);
void substract(*[]);

//predicate functions

bool isEqualto(int *[]);
bool isNotEqualto(int *[]);
bool isGreaterThan(int *[]);
bool isLessThan(int *[]);
bool isGreaterThanOrEqualTo();
bool isLessThanOrEqualTo();

bool isZero();

private :

int a[40];

};//end class HugeInteger

#endif
Last edited by LuciWiz : 07-Mar-2005 at 13:24. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 08-Mar-2005, 02:03
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
I don't understand what you are trying to do here:

CPP / C++ / C Code:
void add(*[]);

What is the type of the parameter?

Following are some personal recommendations. Other programmers like to go a different way about it, so please don't take it as a must-follow opinion.

I recommend you use the name of the variable in the definition as well as in the implementation. This way, if someone receives a .dll from you and your header (as they should if you are into building dlls), they should be able to know how to use your objects and methods.

If they will see:
CPP / C++ / C Code:
void input(int []);

that might be somewhat clear, but with

CPP / C++ / C Code:
void input(int inputArray[]);

you can't miss.

Why have a constructor like this?

CPP / C++ / C Code:
HugeInteger(int [] = 0);//default constructor

I know it is often used like this, and I'm not arguing that you shouldn't, but I personally avoid using default parameters as much as possible (they do make sense sometimes) and prefer method overloading instead.

CPP / C++ / C Code:
HugeInteger();//default constructor
HugeInteger(int someArray[]);//overloaded constructor

And the code for the default case would be treated separately in the default constructor. It seems cleaner this way (to me, at least).

Also, I ALWAYS specify the destructor of the class. Virtual. You don't have to do this every time, but I always think that I or someone else might try to derive my class and build his own SpecializedHugeInteger. But then, when he tries to destroy an instance of his class, he will NOT destroy the data members that have been dynamically allocated in the base class (and inherited in the child). That is because the base destructor will never be called, unless it has been specified virtual. So the proper cleanup will never be done and this is really dangerous.
I would probably write it like this:

CPP / C++ / C Code:
HugeInteger();//default constructor
HugeInteger(int someArray[]);

virtual ~HugeInteger();

Now, we come to your member data:
CPP / C++ / C Code:
private :
	int a[40];

I know this is just a sketch, but you should try to name your variables specific names. a won't do it
Are you sure you need all 40 elements in the array? I know ints are not large... Well, in case you had an array of larger objects I would have suggested to use dynamic memory allocation. This would be a good exercise though. Try to allocate your array in the constructor and free the memory in the destructor:

To give you an idea:

HugeInteger.h
CPP / C++ / C Code:
//...
	HugeInteger();//default constructor
	HugeInteger(int someArray[]);
	HugeInteger(int arraySize);
	virtual ~HugeInteger();
//...

HugeInteger.cpp
CPP / C++ / C Code:
HugeInteger::HugeInteger()
{
	pNumberArray = null;
}

HugeInteger::HugeInteger(int arraySize)
{
	pNumberArray = new int[arraySize];
}

HugeInteger::~HugeInteger()
{
	if ( pNumberArray != null )
	{
		delete [] pNumberArray;
		pNumberArray = null;
	}
}

You might want to develop a convention in naming your member data. I would have actually used [b]m_pNumberArray[/b[, but this is more like the Microsoft way (some say - again, I don't agree). You can choose any other recommend in your documentation. There is the simple preceding underscore version (_pNumberArray), but this might be confusing because it it used in some standard headers as well...

I don't understand why you make use of pointer arrays in some of your member functions. Are you trying to pass by reference? There is no need for this. The other version works:

CPP / C++ / C Code:
void test(int arrayParameter[])
or
CPP / C++ / C Code:
void test(int * arrayParameter)

If you will pass your array's address like this
CPP / C++ / C Code:
 
test(&array[0]);
or
CPP / C++ / C Code:
 
test(array);

it's OK. That's passing by reference!

What I liked about it:
  • You made good use of encapsulation (your member data is private in the class and only accessible through public accessors)
  • The preprocessor directives are a nice touch
    CPP / C++ / C Code:
    #ifndef HugeInteger_H
    #define HugeInteger_H
    //...
    #endif
    
    If you or someone else will build a complicated hierarchy, the header might prove to be hard to include in the necessary places. This will avoid conflicts.

  • You made use of const methods where you were supposed too. Maybe not everywhere, I'm not sure until I see your implementation and/or some class documentation, but you did really good.

I hope others will share with you their personal view on this, as not everyone should agree with mine. I also expect your comments and the implementation of your class. Have fun!

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 14:51
I need some anti-hacking advice! slayerbeatch Open Discussion Forum 6 16-Feb-2005 17:59
Looking for help and advice pcxgamer Open Discussion Forum 1 21-Nov-2003 04:25
HELP! I need advice jmb Web Design Forum 3 25-Mar-2003 04:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.