先看一种情况:注意橘色的部分,是用的类名定义

#include <iostream>
using namespace std;

class Hen
{
public:
    void display()
    {
        cout<<"this is Hen display()"<<endl;
    }
    class Nest
    {
    public:
        int Egg;
        void display()
        {
            cout<<"this is Nest display()"<<endl;
        }
    };
};


int main()
{
    Hen hen;
    hen.display();
    Hen::Nest nest;
    nest.display();

    return 0;
}

另外一种情况:

#include <iostream>
using namespace std;

class Nest
{
public:
    int Egg;
    void display()
    {
        cout<<"this is Nest display()"<<endl;
    }
};

class Hen
{
public:
    void display()
    {
        cout<<"this is Hen display()"<<endl;
    }
    Nest nest;
};


int main()
{
    Hen hen;
    hen.display();
    hen.nest.display();

    return 0;
}

相关文章:

  • 2021-08-01
  • 2022-03-07
  • 2022-12-23
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-24
  • 2021-09-13
  • 2021-12-29
  • 2021-11-15
相关资源
相似解决方案