Thread: A tricky(maybe simple) question

  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
    28,413
    Perhaps you want to make it a function template?
    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
    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
    28,413
    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.
    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

  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
    28,413
    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..
    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
    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
    28,413
    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?
    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

  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
    28,413
    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.
    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. 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, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM