#Style
Style
#Mixed case file names
Mixed case file names
#Factory.h
Factory.h
#Number.h
Number.h
#Number.cpp
Number.cpp
#Fraction.h
Fraction.h
#Fraction.cpp
Fraction.cpp
#Integer.h
Integer.h
#Display.h
Display.h
#main()
main()
#Style
#Mixed case file names
#Factory.h
#Number.h
#Number.cpp
#Fraction.h
#Fraction.cpp
#Integer.h
#Display.h
#main()
Style
Mixed case file names
Factory.h
Number.h
Number.cpp
Fraction.h
Fraction.cpp
Integer.h
Display.h
main()
#Mixed case file names
You should avoid capital letters in file names. Windows
and MacOS
support case insensitive file names, but Linux
and most UNIX
-es - do not.
Imagine someone compress the code using ZIP
or ARJ
and when you un-compress it - surprise - capital/small letters are lost.
Once I lost all small letters on huge Java project - file names became all-caps - it was easy fixable since all I needed to do is to rename everything with small letters.
Keep class names with capital letter, but make files with small letters. C++
allow this, Java
do not.
This is huge problem in Java
world, but nobody speaking about it.
#Factory.h
no need to provide default c-tor. it can be committedremoved.
#Fraction.cpp
prefer initialize list:
Fraction::Fraction(const int & num, const int & den)
{
_numerator.setValue(num);
_denominator.setValue(den);
}
must be:
Fraction::Fraction(const int & num, const int & den) :
_numerator(num),
_denominator(den)
{
}
I would even move it to the Fraction.h
file.
#Factory.h
no need to provide default c-tor. it can be committed.
#Fraction.cpp
#Mixed case file names
You should avoid capital letters in file names. Windows
and MacOS
support case insensitive file names, but Linux
and most UNIX
-es - do not.
Imagine someone compress the code using ZIP
or ARJ
and when you un-compress it - surprise - capital/small letters are lost.
Once I lost all small letters on huge Java project - file names became all-caps - it was easy fixable since all I needed to do is to rename everything with small letters.
Keep class names with capital letter, but make files with small letters. C++
allow this, Java
do not.
This is huge problem in Java
world, but nobody speaking about it.
#Factory.h
no need to provide default c-tor. it can be removed.
#Fraction.cpp
prefer initialize list:
Fraction::Fraction(const int & num, const int & den)
{
_numerator.setValue(num);
_denominator.setValue(den);
}
must be:
Fraction::Fraction(const int & num, const int & den) :
_numerator(num),
_denominator(den)
{
}
I would even move it to the Fraction.h
file.
Code is way too complicated, but let me try:
#Style
you should NOT use names starting with underscore. I did same error in the near past.
If you want to emphasize that those are private
,
add the underscore at the end.
#Factory.h
no need to provide default c-tor. it can be committed.
#Number.h
I don't think this compiles.
virtual Integer * isInteger(){return 0;}
virtual Fraction * isFraction() { return 0; }
You need to use class Integer;
prior class Number
declaration.
Because you are playing with dynamically allocate objects + polymorphism, you MUST add virtual d-tor
. I do not think the code will work correctly without it.
virtual ~Number(){}
I do not like these two:
virtual Integer * isInteger(){return 0;}
virtual Fraction * isFraction() { return 0; }
Is this conversion or just check if class is Integer / Number?
Naming is unfortunate. Change it it toInteger()
or change return type to bool
.
What is return 0
? If it is a pointer, change it to return NULL;
or if you are using C++11
to return nullptr;
.
#Number.cpp
way too big, skipping for the moment.
#Fraction.h
once again this will not be compilled, because you are using unknown class Integer
.
default c-tor
must initialize the _numerator
and _denominator
. I suggest following:
Fraction() : _numerator(0), _denominator(1)
{}
or much better:
class Fraction : public Number
{
Integer _numerator = 0;
Integer _denominator = 1;
//...
Fraction() = default;
What is the d-tor
doing? Is this because of incomplete class Integer
.
C++11
comment - remove virtual
, use override
or final
.
final
helps the optimizer a lot to speed up the code, when class is known.
division by zero
- I think you need to check somehow for it. Probably in c-tor
.
#Fraction.cpp
skipping for now
#Integer.h
same considerations for the c-tor
and d-tor
. Initialize the value to zero.
If you do so, you can ommit initialization of Fraction::_numerator
.
d-tor
once again doing nothing. this time you can remove it.
#Display.h
remove c-tor
and d-tor
.
on all your code, you use pointers
. now suddenly you switch to refferences
.
Why? Answer is - to make main()
more ugly :)
#main()
Do you think there will be exception thrown?
std::auto_ptr
is deprecated. If you are using C++11
, change it to std::unique_ptr
.
Make std::auto_ptr<Number>
to be typedef
. You really not need to write it several times.