Hi,
There are some good things already in your code, you have split the declaration & implementation into header and .cpp files which is great. And you have header guards, which is also good news :+) Plus you used code tags, which I am insanely happy about!
Some other points:
I know you are just starting out, a lot of the following might seem too advanced - I will mention it anyway, plenty for you to think about for your future coding.
Don't have
using namespace std;
, it brings in the entire STL into the global namespace, defeats the purpose of name spaces. Instead put
std::
before each STL variable, class, function, contaner, algorithm etc. Like you did with
std::string
Choose meaningful variable names:
Value
,
INum
,
INeum
aren't as good as they could be. Don't be afraid to make names longer if it helps with the meaning - for example I might have named
convertNum
as
ConvertDecimalToRoman
. That might seem a bit extreme for this small program (might have got away with DecimalToRoman say), but it is a good habit to get into - especially for larger programs. Ideally you want the code to be self documenting - that is the name of the variable or function tells one what is happening. You can use comments to explain expected valid ranges of variables, pre or post conditions, or anything thing else which could be useful, including web addresses for supporting documentation or methodology.
For example you could restrict your input to positive numbers (check that they are) and make the type
unsigned int
. At the moment your code does a simple look up for the numbers 1 to 20, so you should restrict the input to this, or at least cater for invalid input. What happens if the user inputs 50? You should do this validation before creating the object in main() - do it with a function.
There is a bit of a convention for naming member variables with a preceding
m_
. It is handy to be to differentiate between member variables and those that are parameters of functions (function arguments).
With your header file, you have some functions defined inline, there is no need to redefine them in the .cpp file.
There is no need for the virtual destructor at this stage. An ordinary destructor will do.
The
convertNum
function is
private
so you can't call it directly form an object (
Value
), so this function should be
public
You have
convertNum
returning a
std::string
, but on line 23 it returns a
bool
(
true
/
false
)
On line 17, no need to re-declare variable
i
On line 24 you return a char, not a string.
char romeNeum[21];
is an array of char, but on lines 18 to 22 you assign them strings. To assign a
char
use single quotes, but there can only be 1 not any more. So
romeNeum
should be an array of
std::string
.
You can assign array values directly with braces (in a .cpp file, or in a .h file with C++11) like this:
const std::string RomanNumeral[] = {"0", "I","II", "III", "IV"} //etc
On line 23 in the if statement you have assignment not comparison. comparison is
==
If a class function doesn't modify any member variables, it can be marked const as well:
std:: string ConvertDecimalToRoman(unsigned int DecimalValue) const;
With
const
correctness, if it's not going to or shouldn't change make or mark it
const
With constructors, you can use an initialiser list to assign values before the body of the function is executed:
1 2 3 4 5 6
|
ConvertToRome() : // colon introduces initialiser list
m_DecimalValue(0); {}
ConvertToRome(const unsigned int Decimal) : // colon introduces initialiser list
m_DecimalValue(Decimal); {} // if more than 1 separate with comma's do in the same order as
// listed in the class declaration
|
One other huge big golden rule is to always initialise your variables with something.
Now with the overall approach of your code. You do a simple look up of the values 1 to 20, is that what was specified in the assignment? There are ways of looking at the units, tens, hundreds, thousands values of your decimal number, convert them to roman and build a string for the entire number.
There you go lots to think about :+D
Hope all goes well, any questions just ask !