473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

static data members - declaration and inheritance

I am having a lot of trouble writing this program. It is basically keeping track of sales. But I am not getting any values passed from the main function to the class functions. ex Id is not passing to the func JustSold. I am also having trouble with my static variable TotalSales. Can anyone help?....

Thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>   
  3. using namespace std;
  4.  
  5. #define W setw
  6.  
  7. class HotDogStand{
  8.     private:
  9.         static double TotalSales;
  10.     public:
  11.         int StandID;
  12.         int StandSales[];
  13.         void Setup ();
  14.         void CreateStand ();
  15.         void JustSold (int);
  16.         void ShowSales (int);
  17.         int Menu ();
  18.         static void ShowTotalSales ();
  19. };
  20.  
  21.     void HotDogStand::Setup (){
  22.         StandID = 0;
  23.     return;
  24.     }
  25.  
  26.     void HotDogStand::CreateStand (){
  27.         char answer[1];    
  28.         cout << "Do you want to create a new stand (y/n) ";
  29.         cin >> answer;
  30.  
  31.         if (answer[0] == 'n' || answer[0] == 'N'){
  32.             return;
  33.         }
  34.  
  35.         ++StandID;
  36.         StandSales[StandID] = 0;
  37.  
  38.         cout << "Stand #" << StandID << " created!" << endl << endl;
  39.  
  40.     return;
  41.     }
  42.  
  43.     void HotDogStand::JustSold (int StandD){
  44.         ++StandSales[StandID];
  45.         ++TotalSales;
  46.     return;
  47.     }
  48.  
  49.     void HotDogStand::ShowSales (int StandID){
  50.         cout << "Stand #" << StandID << " has sold " << StandSales[StandID] << " hotdog(s)! " << endl << endl;
  51.  
  52.     return;
  53.     }
  54.  
  55.     void HotDogStand::ShowTotalSales (){
  56.         cout << TotalSales << " hotdog(s) have been sold!" << endl;
  57.  
  58.     return;
  59.     }
  60.  
  61.     int HotDogStand::Menu (){
  62.         int option;
  63.  
  64.         cout << "Welcome, what would you like to do: " << endl;
  65.         cout << W(7) << "1." << W(4) << "Create New Stand" << endl;
  66.         cout << W(7) << "2." << W(4) << "Report New Sale" << endl;
  67.         cout << W(7) << "3." << W(4) << "Show Sales of Specific Stand" << endl;
  68.         cout << W(7) << "4." << W(4) << "Show Total Sales" << endl;
  69.         cout << W(7) << "5." << W(4) << "Quit" << endl;
  70.  
  71.         cin >> option;
  72.  
  73.     return option;
  74.     }
  75.  
  76.  
  77. int main(void){
  78.     int option, ID;
  79.  
  80.     HotDogStand A;
  81.     A.Setup();
  82.  
  83.     while (true){
  84.  
  85.         option = A.Menu();
  86.  
  87.         if (option == 1){
  88.             A.CreateStand();
  89.         }
  90.         else if (option == 2){
  91.             cout << "Please Enter Stand ID#: ";
  92.             cin >> ID;
  93.             A.JustSold (ID);
  94.         }
  95.         else if (option == 3){
  96.             cout << "Please Enter Stand ID#: ";
  97.             cin >> ID;
  98.             A.ShowSales (ID);
  99.         }
  100.         else if (option == 4){
  101.             A.ShowTotalSales();
  102.         }    
  103.         else if (option == 5){
  104.             return 0;
  105.         }        
  106.    }
  107.    return 0;
  108. }
Feb 2 '07 #1
6 2200
horace1
1,510 Expert 1GB
try
Expand|Select|Wrap|Line Numbers
  1. class HotDogStand{
  2.     private:
  3.         static double TotalSales;  // declare satic data member
  4.     public:
  5.         int StandID;
  6.         int StandSales[10];
  7.         void Setup ();
  8.         void CreateStand ();
  9.         void JustSold (int);
  10.         void ShowSales (int);
  11.         int Menu ();
  12.         static void ShowTotalSales ();
  13. };
  14. double HotDogStand::TotalSales=0;  // define TotalSales data member
  15.  
  16. etc
  17.  
you declare satatic data members inside the class declaration but
you have to define the static data members outside the class declaration.

also you need to allocate some storage to StandSales[]

you should now be able to get a bit further with the program
Feb 2 '07 #2
thanks a lot, sometimes after a while you skip over little things.

thanks again
Feb 2 '07 #3
hey i have one more question, now that things are working, for some reason when i try to create the second stand I says like stand #22093223 created, why not #2. it also does this in the ShowSales func it says stand #1 has sold #22320492 hotdogs.....
Feb 2 '07 #4
horace1
1,510 Expert 1GB
hey i have one more question, now that things are working, for some reason when i try to create the second stand I says like stand #22093223 created, why not #2. it also does this in the ShowSales func it says stand #1 has sold #22320492 hotdogs.....
you probably need to initialise your data members to 0 - in particular the elements of StandSales[] which will be undefined
Feb 2 '07 #5
vinothg
21
You have a static variable as a data member in your class.Static variables will not be initalized by the constructor.It has to be explicitly define it.

You need to add this line.
HotDogStand::TotalSales = 0;

If you still have queries refer
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10
Feb 2 '07 #6
horace1
1,510 Expert 1GB
the cause of the segmentation faults you were getting was difficult to find. The problem was in
Expand|Select|Wrap|Line Numbers
  1.         char answer[1];    
  2.         cout << "Do you want to create a new stand (y/n) ";
  3.         cin >> answer;
  4.  
answer is an array so you should read a character into it using an index
Expand|Select|Wrap|Line Numbers
  1.  
  2.         cin >> answer[0];
  3.  
I guess your original code was corrupting some memory location

this now works
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>   
  3. using namespace std;
  4.  
  5. #define W setw
  6.  
  7. class HotDogStand{
  8.     private:
  9.         static double TotalSales;  // declare satic data member
  10.     public:
  11.         int StandID;
  12.         int StandSales[50];
  13.         void Setup ();
  14.         void CreateStand ();
  15.         void JustSold (int);
  16.         void ShowSales (int);
  17.         int Menu ();
  18.         static void ShowTotalSales ();
  19. };
  20.  
  21.     double HotDogStand::TotalSales = 0;  // define TotalSales data member
  22.  
  23.  
  24.     // Initializes variable StandID to Zero
  25.     void HotDogStand::Setup (){
  26.         StandID = 0;
  27.     return;
  28.     }
  29.  
  30.     //Creates Different Stands and Reserves a Space for them in the array StandSales
  31.     void HotDogStand::CreateStand (){
  32.         char answer;       //** no need for an array
  33.         cout << "Do you want to create a new stand (y/n) ";
  34.         cin >> answer;
  35.  
  36.         if (answer == 'n' || answer == 'N'){
  37.             return;
  38.         }
  39.  
  40.         ++StandID;
  41.  
  42.         StandSales[StandID] = 0;
  43.  
  44.         cout << "Stand #" << StandID << " created!" << endl << endl;
  45.  
  46.     return;
  47.     }
  48.  
  49.     //Incriments the stands sales and the total sales by one
  50.     void HotDogStand::JustSold (int StandID){
  51.         ++StandSales[StandID];
  52.         ++TotalSales;
  53.     return;
  54.     }
  55.  
  56.     //Displays the sales of a specific stand on the screen
  57.     void HotDogStand::ShowSales (int StandID){
  58.         cout << "Stand #" << StandID << " has sold " << StandSales[StandID] << " hotdog(s)! " << endl << endl;
  59.  
  60.     return;
  61.     }
  62.  
  63.     //Displayes the total sales on the screen
  64.     void HotDogStand::ShowTotalSales (){
  65.         cout << TotalSales << " hotdog(s) have been sold!" << endl << endl;
  66.  
  67.     return;
  68.     }
  69.  
  70.     //Displays the menu on the screen and returns the users choice
  71.     int HotDogStand::Menu (){
  72.         int option;
  73.  
  74.         cout << "Welcome, what would you like to do: " << endl;
  75.         cout << W(7) << "1." << W(4) << "Create New Stand" << endl;
  76.         cout << W(7) << "2." << W(4) << "Report New Sale" << endl;
  77.         cout << W(7) << "3." << W(4) << "Show Sales of Specific Stand" << endl;
  78.         cout << W(7) << "4." << W(4) << "Show Total Sales" << endl;
  79.         cout << W(7) << "5." << W(4) << "Quit" << endl;
  80.  
  81.         cin >> option;
  82.  
  83.     return option;
  84.     }
  85.  
  86.  
  87. int main(void){
  88.     int option, ID;
  89.  
  90.     HotDogStand A;
  91.     A.Setup();
  92.  
  93.     while (true){
  94.  
  95.         option = A.Menu();
  96.  
  97.         if (option == 1){
  98.             A.CreateStand();
  99.         }
  100.         else if (option == 2){
  101.             cout << "Please Enter Stand ID#: ";
  102.             cin >> ID;
  103.             A.JustSold (ID);
  104.         }
  105.         else if (option == 3){
  106.             cout << "Please Enter Stand ID#: ";
  107.             cin >> ID;
  108.             A.ShowSales (ID);
  109.         }
  110.         else if (option == 4){
  111.             A.ShowTotalSales();
  112.         }    
  113.         else if (option == 5){
  114.             return 0;
  115.         }        
  116.    }
  117.    return 0;
  118. }
  119.  
Feb 3 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
2
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.