How to Call Function of one class on the object of another class?
21,148
Solution 1
Unless the two classes are related(through inheritance) You cannot do that.
A member functions performs some action on the instance of the class to which it belongs.
You created an object of class A
so you can only call member functions of A
through it.
Grinding an Apple and hoping to get a mango shake, won't really happen right.
Solution 2
Use public inheritance:
class B {
public:
void bar();
};
class A : public B
{ };
int main() {
A a;
a.bar();
}
Author by
user1414276
Updated on July 09, 2022Comments
-
user1414276 almost 2 years
How can I call one method in one class over using another class ?
I have ;
class A { public : foo ( ) ; }; class B { public : bar ( ) ; };
in main :
A data ; // I am creating instance of class A data . bar ( ) ; // but, I am calling a method of another class // how can I do that ?
Note : I could not find a appropriate title. If you have one, feel free to share or edit