Please help me understanding implementing this class

Reply

Join Date: May 2008
Posts: 46
Reputation: robgeek is an unknown quantity at this point 
Solved Threads: 0
robgeek robgeek is offline Offline
Light Poster

Please help me understanding implementing this class

 
0
  #1
Dec 3rd, 2008
I have an assignment to implement a salesperson class.
Here is the layout I got from prof.
My questions are typed in red between function prototypes.

Any help is appreciated.
Thanks.

cClass Salesperson
{
public:
Salesperson(); //default constructor
// I know that a default constructor is suppose to set the values by default but how would I do this implementation for this constructor. I mean what would this constructor set the value for and how.


~Salesperson(); // destructor

Salesperson(double q1, q2, q3, q4);

void getsales();
[COLOR="Red"]// how would I use this function


void setsales(int quarter, double sales);
//And how would I use this function

void printsales();

void ReadFile();

void EditFile();

private:
double slaes[4];//quarterly sales figures
};
Last edited by robgeek; Dec 3rd, 2008 at 4:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Please help me understanding implementing this class

 
0
  #2
Dec 3rd, 2008
These are basic questions that should be explained in your text, and in your lessons.

In short, the default constructor will allocate the date member(s), sales[4] in this case, but will not initialize the array to any specific values. That would occur in a constructor that you create, such as the one you have with four doubles as parameters.

"get" functions normally provide a means for accessing the class's private data, while "set" functions give you the means to modify the private data.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 46
Reputation: robgeek is an unknown quantity at this point 
Solved Threads: 0
robgeek robgeek is offline Offline
Light Poster

Re: Please help me understanding implementing this class

 
0
  #3
Dec 3rd, 2008
Thanks, I actually figured out most of the stuff.
But I have some problem in my code. In the main(), I am trying to print the sales figure, but instead it is printing the address of the sales array. How can I resolve this without using pointers.

Once again,
Thanks.

My code
  1.  
  2.  
  3. #include "stdafx.h"
  4.  
  5. //#ifndef Salesperson_H
  6. //#define Salesperson_H
  7. #include <iostream>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11.  
  12. class Salesperson
  13. {
  14. public:
  15. Salesperson(); // default constructor
  16. ~Salesperson();//destructor;
  17.  
  18. Salesperson(double , double , double , double );//constructor
  19.  
  20. void getSales(); //function to get 4 sales figures from user
  21.  
  22. void setsales(int , double );
  23.  
  24. void printsales();
  25.  
  26. void ReadFile();
  27.  
  28. void EditFile();
  29.  
  30. private:
  31. double sales[4]; // array for quarterly sales figures.
  32.  
  33. };
  34.  
  35. Salesperson::Salesperson()
  36. {
  37. sales [0] = 0.0;
  38. sales [1] = 0.0;
  39. sales [2] = 0.0;
  40. sales [3] = 0.0;
  41.  
  42. }
  43.  
  44. Salesperson::Salesperson(double quart1, double quart2, double quart3, double quart4)
  45. {
  46. sales [0] = quart1;
  47. sales [1] = quart2;
  48. sales [2] = quart3;
  49. sales [3] = quart4;
  50.  
  51. }
  52.  
  53. void Salesperson::getSales()
  54. {
  55. int quarter;
  56. double sales;
  57.  
  58. cout<<"Please input the the quarter number of the year."<<endl;
  59. cin>> quarter;
  60.  
  61. cout<<"Please enter the sales of this quarter."<<endl;
  62. cin>>sales;
  63.  
  64. setsales(quarter, sales);
  65.  
  66.  
  67.  
  68. return;
  69.  
  70. }
  71.  
  72. void Salesperson::setsales(int quart, double sales)
  73. {
  74.  
  75. if(quart == 1)
  76. sales[0] = sales;
  77.  
  78. if(quart == 2)
  79. sales[1] = sales;
  80.  
  81. if(quart == 3)
  82. sales[2] = sales;
  83.  
  84. if(quart == 4)
  85. sales[3] = sales;
  86. sales= sales [0] + sales [1] + sales [2] + sales [3];
  87.  
  88. return;
  89. }
  90.  
  91. Salesperson::~Salesperson()
  92. {}
  93.  
  94. void Salesperson::printsales()
  95. {
  96. cout<<"The total annual sale of the year was: $"<<sales<<endl;
  97.  
  98. return;
  99. }
  100.  
  101. void ReadFile()
  102. {
  103. cout<<"The file contents were: "<<endl;
  104.  
  105. return;
  106. }
  107. void EditFile()
  108. {
  109. cout<<"Please enter a sentence. You have a space for 80 characters."<<endl;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118. Salesperson s;
  119.  
  120.  
  121.  
  122. return 0;
  123. }
Last edited by robgeek; Dec 3rd, 2008 at 7:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Please help me understanding implementing this class

 
0
  #4
Dec 3rd, 2008
You need to keep things straight better.
  1. void Salesperson::setsales(int quart, double sales)
  2. {
  3. if(quart == 1)
  4. sales[0] = sales;
  5.  
  6. if(quart == 2)
  7. sales[1] = sales;
  8.  
  9. if(quart == 3)
  10. sales[2] = sales;
  11.  
  12. if(quart == 4)
  13. sales[3] = sales;
  14. sales= sales [0] + sales [1] + sales [2] + sales [3];
  15.  
  16. return;
  17. }
Using the same name for your parameter as the data member array should be causing a compile error, as the one will mask the other. You should rename the parameter - say, "new_sales".

Why are you adding all the quarterly sales values in this function?

Your printsales( ) function is doing exactly what you tell it, displaying the address of the array. If you want to see each quarter's sales amount, you need to display each element of the array one at a time - best with a nice little for loop. If, on the other hand, you want to display just a total value, you still need that for loop to add up the array elements. Figure out what it is you really want to do.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC