Hi,
Welcome to cplusplus :+)
Just some pointers to help you out for the future:
1. Please use code tags
2. If you have compiler errors, then please post them here verbatim. That saves us trying to do mind reading, or compiling it ourselves. We can explain what the errors mean.
3. Provide a description of where you think it might be going wrong.
4. Provide a description of the assignment question - so we can see what you are aiming for, at the moment we are guessing. I could write that program with much shorter code, but I imagine that your teacher wants you to have practice at doing things in different ways.
5. If you have runtime errors, then consider using a debugger. If using an IDE, there should be a GUI one built-in.
With classes, there is the concept of the invariant - which means that all the member variables have to maintain values that are sensible throughout the lifetime of the object. For example you have
Quantity
and
Price
which must always be positive.
So when you create an object, there are some different choices. One is to get the user input, validate it, then create the object with the valid values. Another way is to create the object with the input values, then in the constructor check the validity of them, then print out error messages and call the destructor if any of them are invalid. There are other ways such using assertions and exceptions - but I am guessing those topics may be a bit advanced at the moment.
Right now you have a bunch of trivial set functions, here is an example of a better way of doing the same thing with an initialiser list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Invoice::Invoice( const std::string& initPartNumber, // Use references for STL containers
const std::string& initPartDescription, // use const for things that shouldn't change
const int initQuantity, // must be positive
const int initPrice) // must be positive and is whole dollar amount
: // colon introduces initialiser list
partNumber (initPartNumber), // member variables are initialised here
partDescription (initPartDescription),
quantity (initQuantity),
price (initPrice)
{ // constructor
if (quantity <= 0 || price <=0 ) {
std::cout << "Error - Price or Quantity must be greater than zero - destructor called\n"
~Invoice();
}
// invariants OK, assign values
invoiceAmount = quantity * price;
}
|
So now you could get rid of all those set functions - unless your teacher wants you to have them (presumably so you can have practice calling them). With the case of having to update values of member variables after the object has been created, you could write an
Update
function, remember that a member function has direct access to member variables. Another example might for classes for various shapes, I would have functions such
Move
,
Rotate
,
Scale
etc, rather than just get / set functions.
So the idea here is to think about what needs to
happen to an object, rather than blindly writing get / set functions for every member variable. Btw, this forum has had huge debates in the past about this topic, hopefully it won't turn into a marathon again ! :+|
It's also possible to get rid of the get functions as well :+) In your example, the values of the member variables don't change and there doesn't seem to be a requirement for the member variables to be accessed from outside the object. So you just write a
PrintDetails
function, that prints all the values nicely. Remember a member function has access to all the member variables.
If there is a requirement to access member variables values, then I don't have a problem with trivial accessor functions.
With your
getInvoiceAmount
function, it should just return the value, and shouldn't take any arguments. Validation and assignment should have been done earlier.
Hope this helps a bit :+D