C++继承机制全解析
立即解锁
发布时间: 2025-08-20 01:44:35 阅读量: 1 订阅数: 3 


C++编程入门:从零基础到游戏开发
### C++ 继承机制全解析
#### 1. 继承的引入
在编程中,我们可能会遇到这样的情况,比如有一个 `Player` 类,其定义如下:
```cpp
class Player
{
ProjectileWeapon myProjectileWeapon;
MeleeWeapon myMeleeWeapon;
MagicalWeapon myMagicalWeapon;
// ... rest of class
};
```
如果玩家一次只能携带一种武器,这种设计就显得很笨拙,而且随着武器类型的增加,管理会变得更加复杂。幸运的是,C++ 提供了继承机制来解决这个问题。继承用于表示 “是一种” 的关系,例如,投射武器是一种武器。通过继承,我们可以创建一个通用的 `Weapon` 类作为基类,`ProjectileWeapon` 类作为派生类继承自它。
#### 2. 编写继承代码
首先,我们来创建基类 `Weapon`:
```cpp
class Weapon
{
int damage;
int cost;
public:
Weapon(int theDamage, int theCost);
// ... more methods
};
Weapon::Weapon(int theDamage, int theCost)
: damage(theDamage), cost(theCost)
{}
```
接着,创建派生类 `ProjectileWeapon`:
```cpp
class ProjectileWeapon : public Weapon
{
int range;
public:
ProjectileWeapon(int theDamage, int theCost, int theRange);
};
```
这里关键的是第一行代码 `class ProjectileWeapon : public Weapon`,它表示 `ProjectileWeapon` 是 `Weapon` 的一种,`Weapon` 类的所有成员会自动包含在 `ProjectileWeapon` 类中。
派生类构造函数的实现需要注意,错误的实现方式如下:
```cpp
// Will not compile. ProjectileWeapon cannot access Weapon’s private members
ProjectileWeapon:ProjectileWeapon(int theDamage, int theCost, int theRange)
: damage(theDamage), cost(theCost), range(theRange)
{}
```
正确的方式是调用基类的构造函数:
```cpp
// Right way. Call the parent’s constructor
ProjectileWeapon:ProjectileWeapon(int theDamage, int theCost, int theRange)
: Weapon(theDamage, theCost), range(theRange)
{}
```
创建派生类的一般语法为:
```plaintext
class derived_class : scope_modifier base_class
```
其中 `scope_modifier` 可以是 `public`、`private` 或 `protected`,这里我们通常使用 `public`。
#### 3. 通过继承访问类成员
类成员有 `public`、`private` 和 `protected` 三种访问修饰符。`protected` 修饰符只有在继承中才会发挥作用,在没有继承时,它和 `private` 相同。
派生类的成员函数可以访问以下内容:
- 所有全局变量。
- 类自身的成员。
- 基类的所有 `public` 和 `protected` 成员。
例如:
```cpp
class Base
{
int private_int;
protected:
int getInt() { return private_int; }
};
class Derived : public Base
{
// Error - cannot access private_int
int myFunc() { return private_int; }
// The right way
int myFunc() { return getInt(); }
}
```
在这个例子中,`Derived` 类不能直接访问 `private_int`,但可以通过基类的 `protected` 成员函数 `getInt()` 来访问。
#### 4. 派生类中构造函数和析构函数的创建
派生类构造函数和析构函数的规则如下:
- 如果基类没有构造函数或有一个构造函数不需要参数,派生类不需要构造函数。
- 如果基类的所有构造函数都需要参数,派生类必须有构造函数,并且在初始化列表中显式调用基类的构造函数。
- 派生类的构造函数不能在初始化列表中初始化从基类继承的变量,必须使用基类的构造函数或在花括号内初始化。
例如:
```cpp
class Base
{
public:
Base() : protected_int(3) {}
protected:
int protected_int;
};
class Derived : public Base
{
public:
Derived();
};
//the Derived class’s constructor
//the right way
Derived::Derived() : Base()
{}
//also right
Derived::Derived() : Base()
{
protected_int = 2;
}
//error
Derived::Derived() : Base(), protected_int(2)
{}
```
构造顺序是先基类后派生类,析构顺序相反。示例代码如下:
```cpp
// 7.1 – Constructors and Destructors – Mark Lee
#include <iostream>
using std::cout;
class Base
{
public:
Base() { cout << “Base’s constructor\n”; }
~Base() { cout << “Base’s destructor\n”; }
};
class Derived : public Base
{
public:
Derived() { cout << “Derived’s constructor\n”; }
~Derived() { cout << “Derived’s destructor\n”; }
};
int main (void)
{
Derived d;
return 0;
}
```
输出结果为:
```plaintext
Base’s constructor
Derived’s
```
0
0
复制全文
相关推荐










