| | |
Please help me understanding implementing this class
![]() |
•
•
Join Date: May 2008
Posts: 46
Reputation:
Solved Threads: 0
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.
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.
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: May 2008
Posts: 46
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" //#ifndef Salesperson_H //#define Salesperson_H #include <iostream> #include <iomanip> using namespace std; class Salesperson { public: Salesperson(); // default constructor ~Salesperson();//destructor; Salesperson(double , double , double , double );//constructor void getSales(); //function to get 4 sales figures from user void setsales(int , double ); void printsales(); void ReadFile(); void EditFile(); private: double sales[4]; // array for quarterly sales figures. }; Salesperson::Salesperson() { sales [0] = 0.0; sales [1] = 0.0; sales [2] = 0.0; sales [3] = 0.0; } Salesperson::Salesperson(double quart1, double quart2, double quart3, double quart4) { sales [0] = quart1; sales [1] = quart2; sales [2] = quart3; sales [3] = quart4; } void Salesperson::getSales() { int quarter; double sales; cout<<"Please input the the quarter number of the year."<<endl; cin>> quarter; cout<<"Please enter the sales of this quarter."<<endl; cin>>sales; setsales(quarter, sales); return; } void Salesperson::setsales(int quart, double sales) { if(quart == 1) sales[0] = sales; if(quart == 2) sales[1] = sales; if(quart == 3) sales[2] = sales; if(quart == 4) sales[3] = sales; sales= sales [0] + sales [1] + sales [2] + sales [3]; return; } Salesperson::~Salesperson() {} void Salesperson::printsales() { cout<<"The total annual sale of the year was: $"<<sales<<endl; return; } void ReadFile() { cout<<"The file contents were: "<<endl; return; } void EditFile() { cout<<"Please enter a sentence. You have a space for 80 characters."<<endl; } int main() { Salesperson s; return 0; }
Last edited by robgeek; Dec 3rd, 2008 at 7:40 pm.
You need to keep things straight better.
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.
C++ Syntax (Toggle Plain Text)
void Salesperson::setsales(int quart, double sales) { if(quart == 1) sales[0] = sales; if(quart == 2) sales[1] = sales; if(quart == 3) sales[2] = sales; if(quart == 4) sales[3] = sales; sales= sales [0] + sales [1] + sales [2] + sales [3]; return; }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- Differences Between Java and C/C++ (C++)
- C++ Books (C++)
- Help implementing a binary search tree in Java (Java)
- Extending a class (Python)
- Stack conversion (C++)
- greeting everyone, a little help? thanks. (C++)
Other Threads in the C++ Forum
- Previous Thread: Segmentation Fault error
- Next Thread: loop 'j' in subroutine starterequive
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






