Thread: Assigning a value to a static field

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Assigning a value to a static field

    Code:
    #include <iostream>
    using namespace std;
    
    
    class Order{
        private:
            static double minimumCharge;
            int tableNum;
            string serversName;
            int numOfPatrons;
        public: 
            void getTableNum(int);
            void getNumOfPatrons(int);
            void getServersName(string);
            int displayTableNum();
            string displayServersName();
            int displayNumOfPatrons();
    };
    
    
    double Order::minimumCharge = 4.75;
    
    
    void Order::getTableNum(int num){
        tableNum = num;
    }
    void Order::getNumOfPatrons(int numPt){
        numOfPatrons = numPt;
    }
    void Order::getServersName(string name){
        serversName = name;
    }
    int Order::displayNumOfPatrons(){
        return numOfPatrons;
    }
    int Order::displayTableNum(){
        return tableNum;
    }
    string Order::displayServersName(){
        return serversName;
    }
    int main(void){
        cout << Order::minimumCharge;
        return 0;
    }
    ON the line 21 error: 'double Order::minimumCharge' is private.
    Why is this type of assigning wrong with private fields?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ZeroesAndOnes
    ON the line 21 error: 'double Order::minimumCharge' is private.
    Why is this type of assigning wrong with private fields?
    That is not an error; that is what the error message refers to. The error is in trying to access it directly from within the main function:
    Code:
    cout << Order::minimumCharge;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by laserlight View Post
    That is not an error; that is what the error message refers to. The error is in trying to access it directly from within the main function:
    Code:
    cout << Order::minimumCharge;
    There is no way to access it directly from the main function?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to implement and call
    Code:
    double Order::getMinimumCharge ( void ) {
      return minimumCharge;
    }
    The fact that you made the member variable static doesn't remove any of the 'private' attributes of the member. It's still just as inaccessible to anything declared outside the class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Salem View Post
    You need to implement and call
    Code:
    double Order::getMinimumCharge ( void ) {
      return minimumCharge;
    }
    The fact that you made the member variable static doesn't remove any of the 'private' attributes of the member. It's still just as inaccessible to anything declared outside the class.

    Yes I am aware of that way of accessing private data.
    In the task it said:
    "Write a class definition for an Order class for a nightclub that contains a table number,a server’s name, and the number of patrons at the table. Include a private static datamember for the table minimum charge, which is $4.75. Write a main()function that delcares NO Order OBJECTS, but that uses a static member function to display the tableminimum charge."

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read carefully: "Write a main()function that delcares NO Order OBJECTS, but that uses a static member function to display the tableminimum charge."

    That is, you are not supposed to access Order::minimumCharge directly, but via a static member function that is public.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by laserlight View Post
    Read carefully: "Write a main()function that delcares NO Order OBJECTS, but that uses a static member function to display the tableminimum charge."

    That is, you are not supposed to access Order::minimumCharge directly, but via a static member function that is public.
    So this is the program using static member function?

    Code:
    #include <iostream>
    using namespace std;
     
     
    class Order{
        private:
            static double minimumCharge;
            int tableNum;
            string serversName;
            int numOfPatrons;
        public: 
            void getTableNum(int);
            void getNumOfPatrons(int);
            void getServersName(string);
            int displayTableNum();
            string displayServersName();
            int displayNumOfPatrons();
            double returnMinimumCharge();
            static void displayMinimumCharge();
    };
     
     
    double Order::minimumCharge = 4.75;
     
     
    void Order::getTableNum(int num){
        tableNum = num;
    }
    void Order::getNumOfPatrons(int numPt){
        numOfPatrons = numPt;
    }
    void Order::getServersName(string name){
        serversName = name;
    }
    int Order::displayNumOfPatrons(){
        return numOfPatrons;
    }
    int Order::displayTableNum(){
        return tableNum;
    }
    string Order::displayServersName(){
        return serversName;
    }
    double Order::returnMinimumCharge(){
    	return minimumCharge;
    }
    void Order::displayMinimumCharge(){
    	cout << "Minimum charge: " << minimumCharge;
    }
    int main(void){
       	
       	Order::displayMinimumCharge();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ public: static field error
    By Bingo90 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2014, 06:14 AM
  2. error: object ref. required for non static field
    By Dale in forum C# Programming
    Replies: 3
    Last Post: 11-10-2011, 10:48 AM
  3. Replies: 1
    Last Post: 05-31-2009, 04:02 PM
  4. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  5. assigning to static map<>s in classes
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2005, 09:05 PM

Tags for this Thread