Access to methods of another (external) class

This is a discussion on Access to methods of another (external) class within the C++ Programming forums, part of the General Programming Boards category; I (beginner) am trying to program a simple calculator that accepts two numbers (float) and can add, subtract, multiply and ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    8

    Access to methods of another (external) class

    I (beginner) am trying to program a simple calculator that accepts two numbers (float) and can add, subtract, multiply and divide.

    I want to realise this “project” using at least 3 classes. According to my design these classes are:

    Class Input (reads inputs from the user. the inputs are numbers and operations “+”, “-“, “*” and “/”)

    Class Operation (gets the entered data from the input class and accomplishs the calculation)

    Class Display (gets the results from the Class Operation and displays on the screen).

    The question is how can I implement the transfer of messages say from class Input to class Operation or to class Display?

    I highly appreciate if I get a sample code for such class interactions.

    Thanks in advance

    Praul

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    well there are several ways of doing this. One might be to make Input and Display friends of Operation. That way they can directly set/read member variables of Operation.

    Personally, I would only use Operation and then overload cin >> and cout << functions for Operation to do the input/output.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You could have (an) Input object(s) be member variable(s) of the Operation class and you could pass an Operation object to a Display object method.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Quote Originally Posted by elad
    You could have (an) Input object(s) be member variable(s) of the Operation class and you could pass an Operation object to a Display object method.
    That was my idea. But as I mentioned earlier I am starting from scratch. I have read and am reading yet how to do that. But i could not get any workable (implementation) code. e.g.

    Class Input

    public:
    float get_first_number()
    private:
    float first_number;
    void setfirst_number(float);


    If the above class declaration is valid, then I want to transfer this input namely the value of the first_number to the method of the class operation.

    The problem is (or what I do not know is) hot to do it.

    Can you please show me the implementation for the above example? Thank you so much.

    Praul

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Could you nto use inheritance?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Inheritance doesn't seem to make sense in this case.

    You first have to figure out where to create and keep an instance (or instances) of each of your classes. Perhaps they will go into main. Then you need to identify how one interacts with another. One simple way would be to pass the operation class to the input class as a parameter to an input function so that it can be filled with data.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    If you ask me, using classes isnt the way to go. Dont use them just because you can, use them because they help you solve the problem.

    In this case I would use a couple a functions. The functions I would use would probably be
    - GetInput
    - ParseString
    - CalcResult
    And then I would simply display it in main with a single cout. Using classes really seems to over-complicate things since you are trying to use classes in a way they are not design to be used (IMO).

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree that classes shouldn't be used in the real world just because they are available. However, if you are trying to learn and you've chosen a project for the educational value, not for efficiency or appropriateness, then it seems fine to me.

    I might try something like this:
    Code:
    //rough pseudocode
    
    struct Input 
    operand1
    operand2
    operator
    void obtain()
    {
       //obtain first operand
        //obtain operator
       //obtain operand2
    }
    
    class Operation 
    private:
    Input input;
    float result;
    
    public:
    
    void perfom()
    {
       input.obtain();
       switch(input.operator)
       { 
           case '+':
              //result is sum of input.operand1 and input.operand2
           //etc
        }  
    }
    
    class Display
    void doit(const Operation & o) 
    {
     //displays o.result
    }
    
    int main
    Operation op;
    op.perform();
    Display d;
    d.doit(op);
    Should I be more protective of variables, sure. Is it a klunky solution to a common problem, sure. Is it be a useful learnng exercise in how classes can interact with other, only the OP can say.
    You're only born perfect.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Quote Originally Posted by elad
    I agree that classes shouldn't be used in the real world just because they are available. However, if you are trying to learn and you've chosen a project for the educational value, not for efficiency or appropriateness, then it seems fine to me.
    Thank you very much indeed. I prefer this approach as you said for the sake of learning only.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Are struct and class the same or they differ in essence and in implementation? Why did you prefer to use struct for Input?

  11. #11
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I think a good way to learn classes is to make a simple text-based console game.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Are struct and class the same or they differ in essence and in implementation?
    struct and class are the same except that by default, struct members (and base classes) are public and class members (and base classes) are private. If you always specify public or private then they are the same. In practice, people generally use structs for bundles of data that don't really have any behavior associated with them, so people often use structs without any member functions and then use a class if they need to add member functions and hide the data behind an interface.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Thank you so much!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio - Access PictureBox outside Form1 class?
    By andreasleon in forum C# Programming
    Replies: 1
    Last Post: 06-08-2009, 01:55 PM
  2. Access protected base class template variable
    By pjotr in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2007, 04:34 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21