14-14.txt

来自「c和c++完美演绎,里面有编程的方法,对编程技术的提高有很大的帮助」· 文本 代码 · 共 42 行

TXT
42
字号
/* 范例:14-14 */
#include <iostream.h>
class A
{
  private:
    class B
    {
      public:
      int c;
      int d;
      /* 属于“嵌套类”的成员函数参数列设定“外围类”A类型的mythis对象指针
         变量*/
      void Bfun(A* mythis)  
      {
        mythis->a=100;  //存取外围类的成员
        mythis->b=50;
        cout<<"mythis->a="<<mythis->a<<"\tmythis->b="
            <<mythis->b<<endl;
      }
    };
  public:
    B obj2;  /* 以嵌套类B为类型,定义数据成员obj2 */
    int a;
    int b;
    /* 参数行中以嵌套类B为类型,定义区域变量mythis */
    void Afun(B* mythis)
    {
      mythis->c=150;  //存取嵌套类的成员
      mythis->d=200;
      cout<<"mythis->c="<<mythis->c<<"\tmythis->d="
<<mythis->d<<endl;
    }
};
void main()
{
  A obj1;
  obj1.Afun(&obj1.obj2);
  obj1.obj2.Bfun(&obj1); 
  getchar();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?