Hi,
Just some extra things to help you out :+)
Remove the
using namespace std;
from your header file, you don't need it there, in fact it's best practise to remove it everywhere, put
std::
before each std thing. Google as to why that is :+)
Consider using an member initialiser list with a constructor, rather than your
set_values
function. One only needs set functions if the value of a member variable needs to change after the object has been created:
1 2 3 4 5 6 7 8 9 10 11
|
invoice::invoice(const std::size_t Number, // should be positive
const std::size_t Quantity, //and arguments can often be const
const double Price) // prefer double rather than float
: // colon introduces initialiser list
itemNumber(Number),
itemQuantity(Quantity),
itemPrice(Price);
{
// do validation here, check for maximum range
// Price should be positive
}
|
Functions that don't change the state of the object should be marked
const
:
void printInvoiceAmount() const;
This means you are promising not to change the value of any of the member variables. Remember to change it for the function definition, declarations and definitions must match :+)
I like to use .hpp for header files, because it says that the code is c++.
std::endl
flushes the buffer every time so that could be inefficient, (not a problem here though). Use "\n" instead:
std::cout << "The invoice amnout of the first item is: " << amount1 << "\n";
One can build up long
std::cout
statements with several statements:
1 2
|
std::cout << "The invoice amount of the amount1 is 0 because ";
std::cout << "the item quantity is less than or equal zero\n";
|
Avoid global variables like you have on lines 5 and 6, put them in
main()
. It's a good idea to limit the scope of a variable as much as possible, if you can get away with making them local to a function, then do so :+)
When a function has no arguments, there is no need to put
void
, this has never been a requirement for C++, it was for C though.
getInvoiceAmount
should not return an
int
,
double
if you take my earlier advice. There should have been a warning about narrowing types
float
to
int
.
Hope all this helps, even a little bit :+)
Good Luck !!