![]() |
|
#1
|
|||
|
|||
i need an advicei 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:
Last edited by LuciWiz : 07-Mar-2005 at 13:24.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
||||
|
||||
|
I don't understand what you are trying to do here:
CPP / C++ / C Code:
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:
that might be somewhat clear, but with CPP / C++ / C Code:
you can't miss. Why have a constructor like this? CPP / C++ / C Code:
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:
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:
Now, we come to your member data: CPP / C++ / C Code:
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.cpp CPP / C++ / C Code:
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:
CPP / C++ / C Code:
If you will pass your array's address like this CPP / C++ / C Code:
CPP / C++ / C Code:
it's OK. That's passing by reference! What I liked about it:
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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