0

For example in my program I have 2 classes and I want to access the function that is in the other class from another class, is there a way to do this?

  class hello{
  private:
  public:
  void function(){
 // codes
 }
 }
  
  class hi{
  private:
  public:
  void function(){
 // codes i want to access from class hello
 }
 }
Ray Huang
  • 3
  • 2
  • 1
    You need to have an instance of the other class (unless you make them static) – fredrik Oct 19 '20 at 07:09
  • 1
    If it a static function and public you can access it.if is not static and public then only via a object of the other class.non-public functions cannot be accessed. – moon shines Oct 19 '20 at 07:10
  • I think you need to start reading some good beginner's C++ book. – Jabberwocky Oct 19 '20 at 07:12
  • Please make a [mre] to demonstrate. It will provide you with important insight. E.g. that you need an instance of class `hi` to call the method `function()`. Your MRE will force you to create one. That will make a foundation for you to understand what the comments imply, that you also need to have an instance of `hello`. (Skipping the concept of static methods here.) – Yunnosch Oct 19 '20 at 07:17
  • There is a list of decent books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 19 '20 at 07:31

3 Answers3

2

You need use an object of hello to call hello::function() in hi class because hello::function() is not a static function. For example:

class hi {
private:
    hello mHello;
    
public:
    void function(){
        mHello.function();
    }
 };
TuanPM
  • 685
  • 8
  • 29
  • I am afraid that OP cannot see how much you focused on only one of the possible answers to their unfocused question. This might mislead them to believe that this is the only way, which in turn probably causes weird designs in their project. – Yunnosch Oct 19 '20 at 07:14
  • @Yunnosch By the time, they can learn more by themselves. – TuanPM Oct 19 '20 at 07:38
  • I do not really get your point. Unless you mean that "by the time they recovered from confusion and understad the matter they have learned enough to know what you are hiding from them". But you do not need to make it harder for them. – Yunnosch Oct 19 '20 at 07:40
  • @Yunnosch You should help them better than me. – TuanPM Oct 19 '20 at 09:13
  • I will, as soon as the question has been made answerable. My point is that trying to answer earlier by making assumptions and focusing on only a part of the possible solutions to possible interpretations of a question is the wrong path to go with a new contributor. – Yunnosch Oct 19 '20 at 09:51
  • @Yunnosch OK. I appriciate you and respect to you. Let's do that your way – TuanPM Oct 20 '20 at 03:07
1

I'll try to clarify a bit about the role of classes and member functions. If you work with completely abstract names like hello, foo, etc, it can be difficult to connect your learning to anything practical.

Class names tend to be nouns. For example class Dog { ... };. Because they represent types of objects.

Function names tend to be verbs. For example void Bark() { ... }. Because functions do things.

So take this example:

class Dog // define a "Dog" type of object 
{
public:
    void Bark() // give dogs the ability to bark.
    {
        std::cout << "Woof!" << std::endl; 
    }
};

You might be tempted to think you can call Bark() "directly", for example Dog::Bark();. But this is nonsensical because "dog" cannot bark. *A* dog barks. Dog is just a type of object, it's not an object itself. You must instantiate a dog object. Like this:

Dog fido;
fido.Bark();
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Nice answer as it illustrates the key issue. If the OP pays attention they will learn a lot. – john Oct 19 '20 at 07:35
0

It depends on how you are going to handle the class.
If you want a static method, you can do this.

#include <iostream>

class Hello
{
    public:
        static void hello()
        {
            printf("%s", "Hello\n");
        }
};
  
class World
{
    public:
        void world()
        {
            Hello::hello();
            printf("%s", "World\n");
        }
 };

int main()
{
    auto w = World();
    w.world();
}

Otherwise, You can do this.

#include <iostream>

class Hello
{
    public:
        void hello()
        {
            printf("%s", "Hello\n");
        }
};
  
class World
{
    public:
        void world()
        {
            auto h = Hello();
            h.hello();
            printf("%s", "World\n");
        }
 };

int main()
{
    auto w = World();
    w.world();
}
Minu
  • 380
  • 2
  • 12