For example we have a class called Treatment. then in that class we have 2 functions that is treat() and treatmentStaff(). Function treatmentStaff() is called from treat().
In main, we create 2 objects for that class, namely Treatment treatmentA, treatmentB;
then we assign treatmentA.treatmentStaff(); , treatmentB.treatmentStaff();

I wanna ask, can we call the “treatingStaff()” method of “treatmentB” from the “treat()” method of “treatmentA”?

Thanks for the reply.

You sure can. It'll do the same thing it would if you had called it from anywhere else in the code.

But... you shouldn't. It's kind of like Steve Jobs calling up Bill Gates and telling him how to run his business. There isn't any reason peers should be sticking their noses in each other's private places.

What exactly are you trying to do? Perhaps we can suggest a better way.

Actually it is a question from my lecturer. He wants us to find out whether it can be done or not. I think it can not be done. But I am not sure also. ha9. So, I try to ask somebody more expert than me in this forum whether this thing can happen or not. So, it can be done but it is not a good practice is it?

Methods do not belong to an object, they belong to class.
>>I wanna ask, can we call the “treatingStaff()” method of “treatmentB” from the “treat()” method of “treatmentA”?
This is a wrong question. "method of “treatmentB" does not mean anything.

Any member function of a class can be called from other member function. Therefore if treatingstaff is a member function of the class treatment you can call it from any other member function including treat().

Now, if I understand your questions correctly if you call treat() using treatmentA it can call the member function treatingstaff() but it will act on object treatmentA not on treatmentB.

dubeyprateek
Don't be confusing, and don't give people gruff for nonsense.

A class is a type of thing. An object is an instance of that thing. A method is a function associated with a class (or type of thing), so objects of that type of thing (or class) can certainly be considered as "owning" a method. Remember, a class doesn't exist --but an object does.

Also, pay attention to the OP's question: she's not interested in calling methods "belonging to" the caller.

need_Direction
You call the method of any class by first specifying the object's name, then the method name. For example: myobject.dosomething(); This assumes that "myobject" has a parameterless method named "dosomething" associated with it. You can put such a statement anywhere that can see "myobject".

Often you can write a little program to test your hypothesis. Our hypothesis will be: object "B" can call a method of object "A".

#include <iostream>
using namespace std;

class myInt {
  public:
    char name;
    int i;
    myInt( char _name, int _i ): name( _name ), i( _i ) { }
    void print();
    void doSomething();
    void doMore( myInt &anInt );
  };

myInt A( 'A', 10 ), B( 'B', 42 );  // (1)

void myInt::print() {
  // Tell the world who I am and my value
  cout << name << ' ' << i << endl;
  }

void myInt::doSomething() {
  // Tell the world who A is (no matter who I am), and value
  A.print();
  }

void myInt::doMore( myInt &anInt ) {
  // Tell the world about anInt (no matter who I am)
  anInt.print();
  }

int main() {
  myInt C( 'C', 89 );  // (2)

  A.print();
  B.print();
  C.print();
  cout << endl;

  A.doSomething();
  B.doSomething();
  C.doSomething();
  cout << endl;

  C.doMore( A );
  A.doMore( B );
  C.doMore( C );

  return EXIT_SUCCESS;
  }

The only caveat is that the object itself must be known to the method. Notice how I declared A and B at (1)? If I had declared them at (2) then the program wouldn't compile, because the doSomething() method wouldn't know what "A" was...

However, the doMore() method compiles just fine, because I passed it a reference instead of using an object name directly in its definition.

Why don't you give this a compile and run and see what each of the statements in main() print?

Hope this helps.

commented: You really took time to explain this. +2

u r so helpful...thank you very much...i think i understand olredi...^^
thanks alot...

This is very important aspect of oops and not clear most of the programmers. I have Already
Faced that Question So I Searched This questions in various sites and the result is as.

For Better understanding let we have three file
classone file for first class name as classone.php
classtwo file for class two and name as classtwo.php
and a file to show the result its name is index.php

classone.php have the code for class first as

<?php
class classone
{

// the example function

function functionone()
{
echo 'the result of first class if possible';
}

}

?>

now the second class file
classtwo.php and the code is
<?php
include ('classone.php');

class classtwo
{

function functiontwo()
{
echo 'the output of the second class if possible';

//make a class as an object and get the function
$objectone= new classone();

$objectone->functionone();


}

}

?>


now as i display the result of classone in index.php
the code is

<?php
include 'classtwo.php';

$objecttwo= new classtwo();

$objecttwo->functiontwo();

?>

Rakesh Kumar Nautiyal

You do know that this is a year-old thread in a C++ forum?

If you want to contribute, please use C++ (for those who don't understand PHP), and please don't repeat information already presented.

You do know that this is a year-old thread in a C++ forum?

If you want to contribute, please use C++ (for those who don't understand PHP), and please don't repeat information already presented.

I am a PHP guy and present my view only for php Professionals not for C++ but logic will be same

I am a PHP guy and present my view only for php Professionals not for C++

But please note that this is strictly a C++ forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.