#include<iostream>
#include<time.h>
using namespace std;

class animal
{
   public:
   friend class  fish;                //class 此处不能少,否则无法识别fish是类。
      animal(int i):a(i)
      {}
   private:
      int a;
      int add()
      {
         a+=5;
         return a;
      }
};
class fish                        //fish是animal的友元,所以可以使用animal类中的私有成员。
{
   public:
      void print(animal &ani)            //参数中要有animal类的对象
      {
         ani.add();
         cout<<ani.a<<endl;
      }
};

int main()
{
   fish fh;
   int i;
   cin>>i;
   animal an(i);                    //此处定义了animal类。
   fh.print(an);
}

相关文章:

  • 2021-12-12
  • 2021-10-31
  • 2021-04-27
  • 2021-10-28
  • 2021-09-22
  • 2022-02-17
  • 2021-06-18
  • 2021-09-08
猜你喜欢
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2021-11-20
相关资源
相似解决方案