compclass2.h

来自「适合初学者学习以及程序员回顾」· C头文件 代码 · 共 54 行

H
54
字号
// CompClass2.h

#ifndef COMPCLASS2_H
#define COMPCLASS2_H
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;

//---- 宣告类别 Component --------
class Component
{
  private:
    int I;
  public:
    // 类别 Component 的建构函数
    Component (): I(1)
     {cout << "呼叫 Component 预设建构函数" << endl; }
    Component (int N): I(N)
     {cout << "呼叫 Component 建构函数" << endl;}
    // 类别 Component 的解构函数
    ~Component()
     {cout << "呼叫 Component 解构函数" << endl;}
    int Get() const   {return I;}
    void Double()     {I*=2;}
};

//---- 宣告类别 Host --------
class Host
{
  private:
    int k;
    Component C1, C2;
  public:
    Component C3;
    // 类别 Host 的建构函数
    Host() : k(1), C1(1), C2(1), C3(1)
    {cout << "呼叫 Host 预设建构函数" << endl; }
    Host(int L, int M, int N, int P)
           : k(L), C1(M), C2(N), C3(P)
    {cout << "呼叫 Host 建构函数" << endl; }
    // 类别 Host 的解构函数
    ~Host()
      {cout << "呼叫 Host 解构函数" << endl;}
    int  Get() const  {return k;}
    void Double()     {k*=2;}
    void DoubleComp()
      {C1.Double(); C2.Double(); C3.Double();}
    int  GetC1()      {return C1.Get();}
    int  GetC2()      {return C2.Get();}
};

#endif

⌨️ 快捷键说明

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