hybclass.h
来自「适合初学者学习以及程序员回顾」· C头文件 代码 · 共 76 行
H
76 行
// HybClass.h
#ifndef HYBCLASS_H
#define HYBCLASS_H
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
//---- 宣告类别 Component --------
class Component
{
private:
int i;
public:
Component (): i(1)
{cout << "呼叫 Component 预设建构函数"
<< endl; }
Component (int N) : i(N)
{cout << "呼叫 Component 建构函数"
<< endl; }
~Component()
{cout << "呼叫 Component 解构函数"
<< endl;}
int Get() const {return i;}
void Double() {i*=2;}
};
//---- 宣告类别 Base --------
class Base
{
private:
int j;
public:
Base(): j(3)
{cout << "呼叫 Base 预设建构函数"
<< endl; }
Base(int N): j(N)
{cout << "呼叫 Base 建构函数"
<< endl; }
~Base()
{cout << "呼叫 Base 解构函数"
<< endl;}
void Set(int N) {j=N;}
int Get() const {return j;}
void Double() {j*=2;}
void Triple() {j*=3;}
};
//---- 宣告类别 Hybrid --------
class Hybrid : public Base
{
private:
int k;
Component C1, C2;
public:
Hybrid() : k(1), C1(3), C2(4), Base(2)
{cout << "呼叫 Hybrid 预设建构函数"
<< endl; }
Hybrid(int L, int M, int N, int P)
: k(L), Base(M), C1(N), C2(P)
{cout << "呼叫 Hybrid 建构函数"
<< endl; }
~Hybrid()
{cout << "呼叫 Hybrid 解构函数"
<< endl;}
int Get() const {return k;}
void Double() {k*=2;}
void DoubleBase() {Base::Double();}
void DoubleComp() {C1.Double(); C2.Double();}
int GetBase() {return Base::Get();}
int GetC1() {return C1.Get();}
int GetC2() {return C2.Get();}
};
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?