RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 809 | Replies: 7 | Thread Tools  Display Modes
Reply
Join Date: Nov 2007
Posts: 9
Reputation: J.P. is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
J.P. J.P. is offline Offline
Newbie Poster

classes function help

  #1  
Nov 18th, 2007
Hi, i'm one of those annoying students here to ask questions

this is what is asked for my assignment
(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data membersa part number (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75)called floating-point valuesto represent dollar amounts.] Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test program that demonstrates class Invoice's capabilities.

& here is what i have so far
  1. #include <iostream>
  2. #include <string>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. using std::string;
  7.  
  8. class Invoice
  9. {
  10. public:
  11. Invoice (string number, string desc, int amount, int price)
  12. {
  13. set_partNumber (number);
  14. set_partDescription (desc);
  15. set_quantityPurchased (amount);
  16. set_priceItem (price);
  17. }
  18.  
  19. void set_partNumber (string number)
  20. {number = PN;}
  21. string get_partNumber()
  22. {return PN;}
  23.  
  24. void set_partDescription (string desc)
  25. {desc = PD;}
  26. string get_partDescription()
  27. {return PD;}
  28.  
  29. void set_quantityPurchased (int amount)
  30. {amount = QP;}
  31. int get_quantityPurchased()
  32. {return QP;}
  33.  
  34. void set_priceItem (int price)
  35. {price = PI;}
  36. int get_priceItem()
  37. {return PI;}
  38.  
  39. void get_invoiceAmount()
  40. {
  41. if (QP < 0)
  42. {QP = 0;}
  43. if (PI < 0)
  44. {PI = 0;}
  45.  
  46. int total = QP*PI;
  47. cout << "Producto#: " << PN << "Descripcion: " <<
  48. PD << "Cantidad: " << QP <<
  49. "Precio: " << PI << "Su total: " << total << endl;
  50. }
  51.  
  52. private:
  53. int QP,PI;
  54. string PN,PD;
  55. };
  56.  
  57. int main()
  58. {
  59. Invoice invoice1 ("1", "motor", 1, 200);
  60. Invoice invoice2 ("2", "cucharas", 50, 3);
  61.  
  62. invoice1.get_invoiceAmount();
  63. invoice2.get_invoiceAmount();
  64.  
  65. system ("Pause");
  66. return 0;
  67. }


do i have to use the get function to get the numbers from the user? (although it doesn't ask for it)
i got the program to run, but not how it's supposed to...i tried using the "get" function in the invoiceAmount function & that doesn't work. also is my constructor made properly?? these books really don't explain much & ask a lot from you. any help greatly appreciated...i thought i was understanding this thing but i'm not getting it at all
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: classes function help

  #2  
Nov 18th, 2007
>>do i have to use the get function to get the numbers from the user?
No -- get functions return the value of private members to the calling function or class. For example get_invoiceAmount() should be an int function that returns the invoice amount (re-read your instructions on this). With your cryptic variable names I can't tell you how to calculate the invoice amount.

I think getting values from keyboard entry should be done in main(), outside the class.

You should add a default constructor (one with no parameters) and initialize all class variables to 0.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2006
Posts: 2,814
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 16
Solved Threads: 240
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: classes function help

  #3  
Nov 18th, 2007
Originally Posted by J.P. View Post
Hi, i'm one of those annoying students here to ask questions
Not as annoying as most new members. At least you used CODE tags, and have attempted to format your code. Good job!!! For additional formatting suggestions, see this. By the way, don't use both INLINECODE and CODE, just CODE


Originally Posted by J.P. View Post
do i have to use the get function to get the numbers from the user? (although it doesn't ask for it)
Not necessarily. getline() makes more sense to me because of the way the get... functions work. Then convert the input to the value.


Also, about system ("Pause") , see this.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Nov 2007
Posts: 9
Reputation: J.P. is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
J.P. J.P. is offline Offline
Newbie Poster

Re: classes function help

  #4  
Nov 18th, 2007
Originally Posted by Ancient Dragon View Post
For example get_invoiceAmount() should be an int function that returns the invoice amount (re-read your instructions on this).
but get_invoiceAmount isn't returning anything...it's working like the displayMessage examples they give in the book
"int get_invoiceAmount" that's what i understand you are telling me to do, am i correct??

With your cryptic variable names I can't tell you how to calculate the invoice amount.
hmmm, i thought it was easy to see
PN - product number
PD - product description
QP - quantity purchased
PI - price of each item

so i have the QP*PI in my get_invoiceAMount, but my data members don't seem to be receiving what i send them in main when i create an object

You should add a default constructor (one with no parameters) and initialize all class variables to 0.

do you mean separating the interface & implementation? putting the constructor into it's own header file?

i'm going to try doing this from the start again cause now i'm getting even more errors lol
BBL
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: classes function help

  #5  
Nov 18th, 2007
Originally Posted by J.P. View Post
but get_invoiceAmount isn't returning anything...it's working like the displayMessage examples they give in the book
It's not supposed to work like displayMessage() -- read your instructions. displayMessage is not supposed to return anything, get_invoiceAmount() is
Originally Posted by J.P. View Post
"int get_invoiceAmount" that's what i understand you are telling me to do, am i correct??
Yes that is right.

Originally Posted by J.P. View Post
hmmm, i thought it was easy to see
PN - product number
PD - product description
QP - quantity purchased
PI - price of each item
Maybe to you because you wrote it.

Originally Posted by J.P. View Post
so i have the QP*PI in my get_invoiceAMount, but my data members don't seem to be receiving what i send them in main when i create an object
you need to do something like this in main()
int invoiceAmount  = invoice1.get_invoiceAmount();


Originally Posted by J.P. View Post
do you mean separating the interface & implementation? putting the constructor into it's own header file?
No, leave the class where it is an just add another constructor.
class Invoice
{
public:
    Invoice() {QP = 0; PI = 0;}
...
};

Originally Posted by J.P. View Post
i'm going to try doing this from the start again cause now i'm getting even more errors lol
BBL

post code
Last edited by Ancient Dragon : Nov 18th, 2007 at 6:37 pm.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 9
Reputation: J.P. is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
J.P. J.P. is offline Offline
Newbie Poster

Re: classes function help

  #6  
Nov 22nd, 2007
sorry Ancient Dragon...i tried following your instructions, but couldn't quite get what you were trying to tell me to do. i've been racking my brain over this damn thing. no matter what i try to come up with i always end up with the same code, maybe cause it's my first time doing this class thing. i fixed it up a bit based on the suggestions here, this is what i have now

  1. #include <iostream>
  2. #include <string>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. using std::string;
  7.  
  8. class Invoice
  9. {
  10. public:
  11. Invoice (string number, string desc, int amount, int price)
  12. {
  13. setPartNumber (number);
  14. setPartDescription (desc);
  15. setQuantityPurchased (amount);
  16. setPriceItem (price);
  17. }
  18.  
  19. void setPartNumber (string number)
  20. {number = product;}
  21. string getPartNumber()
  22. {return product;}
  23.  
  24. void setPartDescription (string desc)
  25. {desc = description;}
  26. string getPartDescription()
  27. {return description;}
  28.  
  29. void setQuantityPurchased (int amount)
  30. {if (amount < 0){
  31. quantity = 0;
  32. }
  33. else {
  34. amount = quantity;}}
  35. int getQuantityPurchased()
  36. {return quantity;}
  37.  
  38. void setPriceItem (int price)
  39. {if (price < 0){
  40. prices = 0;
  41. }
  42. else{
  43. price = prices;}}
  44. int getPriceItem()
  45. {return prices;}
  46.  
  47. int getInvoiceAmount()
  48. {
  49. int total = quantity * prices;
  50. cout << "Producto#: " << getPartNumber() << "Descripcion: " << getPartDescription() << "\nCantidad: " << getQuantityPurchased() << "\nPrecio: " << getPriceItem() << "\nSu total: " << total << endl;
  51. }
  52.  
  53. private:
  54. int quantity;
  55. int prices;
  56. string product;
  57. string description;
  58.  
  59.  
  60. };
  61.  
  62.  
  63. int main()
  64. {
  65. Invoice invoice1 ("1", "motor", 2, 200);
  66. Invoice invoice2 ("2", "cucharas", 50, 3);
  67.  
  68. invoice1.getInvoiceAmount();
  69. invoice2.getInvoiceAmount();
  70. //int invoiceAmount = invoice1.getInvoiceAmount();
  71.  
  72.  
  73. cin.get();
  74. return 0;
  75. }


it still doesn't return anything for my string functions & the int functions return weird numbers...i'm all out of ideas
Reply With Quote  
Join Date: Nov 2007
Posts: 9
Reputation: J.P. is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
J.P. J.P. is offline Offline
Newbie Poster

Re: classes function help

  #7  
Nov 25th, 2007
i solved it!!
everything was right from the start cept that i was assigning the values wrong

  1. void setPartNumber (string number)
  2. {number = product;}
  3. string getPartNumber()
  4. {return product;}

number = product is wrong,it should be:
product = number



you bet i was smacking my forehead when i noticed
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,862
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1013
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: classes function help

  #8  
Nov 25th, 2007
Originally Posted by J.P. View Post
i solved it!!
everything was right from the start cept that i was assigning the values wrong


number = product is wrong,it should be:
product = number



you bet i was smacking my forehead when i noticed


Happens to all of us.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:39 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC