Thread: how can i use a static variable in private session of a class

  1. #16
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    You need to be more specific.


    How do you know that std::map is the appropriate type in the first place? My point here is that the interface of your class looks poorly designed. You need to clarify the purpose of your class first. You need to provide it with a minimal but complete interface. You should not be jumping into the specifics of the implementation without thinking through these issues first.
    You need to be more specific.
    but how!!?

    and about the rest of the post :
    well map is the only thing that i can use to hold (save ) my stuff with regards of lets say line number or a value ..

    and the class. i dont know much about classes, i just read deitels c++ how to program 5th editions, 8th chapter!!.
    the program is divided into some member functions , each function is doing one job, and i tried my best to achieve this goal.
    one member gets the input( the whole program from user (till the user enters exit to exit this part)) ,
    then a parse will parse each line of statements (strings) entered by user once>(the whole program, to locate any labels , declarations , etc) .
    after that it initialized the labels and declaration tabels ,
    the Fetch () member gos online ,it gets one string from StringCollector map, and saves it to OurString (current string for operations from here to the end )
    then an Analyzer will analye the line , it specify the kind of statement we are facing, it saves a number stating the statement type (plain , or with loops , etc0
    in a choice , then Tokenizer () uses this number to do the operation on OurString. then it saves , COMMAND and OFFSET and ADDRESSING MODE stat to their respective variables ,
    then convert to language machin , merges these strings together and makes a unique string ,and Memory Dumper , uses this string and line counter to fill the memory !
    and then it goes back to fetch , till it reaches the end of the mapp!
    and then its time to call Execution Enging () .this member will run codes from memory .!
    well this is the whole stuff im doing , so far parse, analyzer, tokenizer, are implemented! and the other 3 remains.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #17
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    ahhh...sounds so complicated...
    why not simplify it, forget constructor...
    Code:
    public:
    
    CLA ();
    void InputString();
    :
    :
    then:

    Code:
    void CLA::InputString()
            {
                    cout<<"Enter your String Please\n ";
                    getline( cin, OurString  );
    
                    int linecounter = -1;          //counts the number ofstring input by user
                    //OurString = user_input;
                    while (!exitscanner(OurString))
                    {
    
                            //OurString = user_input;
                            StringCollector[linecounter++] = OurString;
                            //cout <<"\n$CLA$ ";                       //shows the user to input again.
                            //getline( cin, user_input );
                            getline( cin, OurString);
                    }
    
            }
    than in your main
    Code:
    int main()
    {
        CLA cla;
        cla.InputString();
        :
        :
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    the program is divided into some member functions , each function is doing one job, and i tried my best to achieve this goal.
    Ah yes, but just as a function should ideally do something and do it well, a class should ideally model one thing and model it well.

    Quote Originally Posted by Masterx
    well this is the whole stuff im doing , so far parse, analyzer, tokenizer, are implemented! and the other 3 remains.
    From what I gather, you are trying to parse some kind of assembly program and possibly simulate running the generated machine code. Clearly, your class does more than just read input

    One approach would be to create say, a Statement class. You can overload operator>> for std::istream in order to read a line of program text from an input stream and produce the corresponding Statement class object. This might use a private member function that parses the line of text and sets the statement type.

    A variation of this approach would eliminate the type code by using a hierarchy of Statement classes. Statement would then be an abstract base class. You might have a virtual function that performs the part that is currently described as "Tokenizer () uses this number to do the operation on OurString". The advantage of this is that it follows the Open-Closed principle: you can add code to handle new kinds of operations without having to modify (much of) the existing code that handles the existing kinds of operations.
    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

  4. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by auralius View Post
    ahhh...sounds so complicated...
    why not simplify it, forget constructor...
    Code:
    public:
    
    CLA ();
    void InputString();
    :
    :
    then:

    Code:
    void CLA::InputString()
            {
                    cout<<"Enter your String Please\n ";
                    getline( cin, OurString  );
    
                    int linecounter = -1;          //counts the number ofstring input by user
                    //OurString = user_input;
                    while (!exitscanner(OurString))
                    {
    
                            //OurString = user_input;
                            StringCollector[linecounter++] = OurString;
                            //cout <<"\n$CLA$ ";                       //shows the user to input again.
                            //getline( cin, user_input );
                            getline( cin, OurString);
                    }
    
            }
    than in your main
    Code:
    int main()
    {
        CLA cla;
        cla.InputString();
        :
        :
    }
    tanx mate.
    Quote Originally Posted by laserlight View Post
    Ah yes, but just as a function should ideally do something and do it well, a class should ideally model one thing and model it well.


    From what I gather, you are trying to parse some kind of assembly program and possibly simulate running the generated machine code. Clearly, your class does more than just read input

    One approach would be to create say, a Statement class. You can overload operator>> for std::istream in order to read a line of program text from an input stream and produce the corresponding Statement class object. This might use a private member function that parses the line of text and sets the statement type.

    A variation of this approach would eliminate the type code by using a hierarchy of Statement classes. Statement would then be an abstract base class. You might have a virtual function that performs the part that is currently described as "Tokenizer () uses this number to do the operation on OurString". The advantage of this is that it follows the Open-Closed principle: you can add code to handle new kinds of operations without having to modify (much of) the existing code that handles the existing kinds of operations.
    (many * many * many) * (infinity) * (tanx) to you dear laiserlight .
    dear ive already thought about that , but because im still a baby in C++ ( or i should have said, infant you know!:d) and im studying C++ on my own, and i havent studied fileing , maps and other stuff in C++, i forgot about that.(evenif i plan to do so, all of my current efforts will be in vein! i have to write another tokenizer from scratch ! and many other things ! the whole algorithm will change
    and thats time consuming ! , i dont have time for that now, )you know , i was at 8th chapter and as an assignment of deitels book, i wrote a simpletron emulator (which was eaaasy.) i thought lets have a more chalenging assignment , and thats how it started . i wanted to emulate CLA, an abstarct computer that belongs to a distand past ( uses Acumulator, and stuff more info : #4)
    well, emulating this system is no big deal, its easy , but the problem is , i havent mastered needed knowledge of C++ to properly implement it the right way.!
    anyway, thanks to you dear and other great members of this forum , that spends your preciuse time to help newbies like me .

    gotta finish this mess as soon as possible and start studying again .

    again thanks a billion to you all dear valued members.
    Last edited by Masterx; 01-26-2009 at 01:06 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #20
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    by the way , why cant we initialize a variable in class? (in private session i mean ? )
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    by the way , why cant we initialize a variable in class? (in private session i mean ? )
    But you can, e.g.,
    Code:
    class X
    {
    public:
        X() : num(0) {} // initialise private member variable num to 0 using initialisation list.
    private:
        int num;
    };
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM