A tricky(maybe simple) question

This is a discussion on A tricky(maybe simple) question within the C++ Programming forums, part of the General Programming Boards category; Okay this might have a simple answer but its not clicking.. How to use the same member function say xyz() ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    13

    Question A tricky(maybe simple) question

    Okay this might have a simple answer but its not clicking..

    How to use the same member function say xyz() to perform 2 different tasks?

    NO..not talking about Function Overloading. The same function is to have codes for both the operations.

    An array is a data member of the class. xyz() should be able to accept the elements of the array when its called and then display it too..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Perhaps you want to make it a function template?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    I dont want to use the function for different data types.. instead want to use it for different operations..

    in case it can be done using function templates then plz tell..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    I am not sure what you mean, but I can tell you that a well written function typically does one thing and does it well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    yeah..its just a question my prof asked..

    said what concept of c++ can be used if we want to do so..

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Use a parameter to select the operation, perhaps?

    You might want to elaborate on this:
    Quote Originally Posted by wayne08
    An array is a data member of the class. xyz() should be able to accept the elements of the array when its called and then display it too..
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    No ..parameter is an obvious way or an if statement in the function..but thats not to be used..

    i'll give a sort of snippet..

    class abc
    {
    int a[10];
    public:
    xyz()
    {
    ...
    ... //accepting code

    ...
    ... //displaying code
    }
    };



    nevermind..the question might be wrong [:P]

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Well... the "accepting code" and "displaying code" part does not make sense. Accept what? Display the array, yes, but what else?

    Or... is this question really about inheritance, polymorphism and virtual function overriding?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    accepting code is to accept the elemets of array..

    for(i=0;i<MAX;i++)
    cin>>a[i];


    display is similarly display..


    its not a proper code to be written ..just a conceptual question. and figuring out what concept can be used is the problem..so dunno..

    let it be..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Ah, as in reading into the elements of the array.

    That's good, but I do not see where the "2 different tasks" comes in. Unless you are saying that the two tasks are precisely the reading from standard input and writing to standard output, in which case the answer is trivial: to use the function, just create an object of the class and call it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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