- C++基础
- C++主页
- C++概述
- C++环境设置
- C++基本语法
- C++注释
- C++ Hello World
- C++省略命名空间
- C++常量/字面量
- C++关键字
- C++标识符
- C++数据类型
- C++数值数据类型
- C++字符数据类型
- C++布尔数据类型
- C++变量类型
- C++变量作用域
- C++多个变量
- C++基本输入/输出
- C++修饰符类型
- C++存储类
- C++运算符
- C++数字
- C++枚举
- C++引用
- C++日期和时间
- C++控制语句
- C++决策
- C++ if 语句
- C++ if else 语句
- C++嵌套 if 语句
- C++ switch 语句
- C++嵌套 switch 语句
- C++循环类型
- C++ while 循环
- C++ for 循环
- C++ do while 循环
- C++ foreach 循环
- C++嵌套循环
- C++ break 语句
- C++ continue 语句
- C++ goto 语句
- C++构造函数
- C++构造函数和析构函数
- C++复制构造函数
C++继承
面向对象编程中最重要的概念之一就是继承。继承允许我们根据另一个类来定义一个类,这使得创建和维护应用程序更加容易。这也提供了重用代码功能和加快实现时间的机会。
在创建类时,程序员可以指定新类应该继承现有类的成员,而不是编写全新的数据成员和成员函数。这个现有类称为**基类**,新类称为**派生类**。
继承的概念实现了**是-a**关系。例如,哺乳动物是动物,狗是哺乳动物,因此狗也是动物,以此类推。
基类和派生类
一个类可以从多个类派生,这意味着它可以继承多个基类的数和函数。为了定义派生类,我们使用类派生列表来指定基类。类派生列表命名一个或多个基类,其形式如下:
class derived-class: access-specifier base-class
其中访问说明符是**public、protected**或**private**之一,基类是先前定义的类的名称。如果未使用访问说明符,则默认为private。
考虑一个基类**Shape**及其派生类**Rectangle**,如下所示:
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
当编译并执行上述代码时,它会产生以下结果:
Total area: 35
访问控制和继承
派生类可以访问其基类的所有非私有成员。因此,基类中不应被派生类的成员函数访问的成员应该在基类中声明为私有。
我们可以根据谁能访问它们来总结不同的访问类型:
| 访问 | public | protected | private |
|---|---|---|---|
| 同一类 | 是 | 是 | 是 |
| 派生类 | 是 | 是 | 否 |
| 外部类 | 是 | 否 | 否 |
派生类继承所有基类方法,但以下情况除外:
- 基类的构造函数、析构函数和复制构造函数。
- 基类的重载运算符。
- 基类的友元函数。
继承类型
从基类派生类时,可以通过**public、protected**或**private**继承继承基类。继承类型由访问说明符指定,如上所述。
我们几乎不使用**protected**或**private**继承,但**public**继承通常使用。在使用不同类型的继承时,会应用以下规则:
**公共继承** - 从**public**基类派生类时,基类的**public**成员成为派生类的**public**成员,基类的**protected**成员成为派生类的**protected**成员。基类的**private**成员永远无法直接从派生类访问,但可以通过调用基类的**public**和**protected**成员来访问。
**受保护的继承** - 从**protected**基类派生时,基类的**public**和**protected**成员成为派生类的**protected**成员。
**私有继承** - 从**private**基类派生时,基类的**public**和**protected**成员成为派生类的**private**成员。
多重继承
C++类可以使用多重继承从多个类继承成员。多重继承是一个允许类从多个基类继承的功能,这意味着派生类可以有多个父类,并继承所有基类的属性和行为。
以下是扩展的语法:
class derived-class: access baseA, access baseB....
其中访问是**public、protected**或**private**之一,并且每个基类都会给出,它们将用逗号分隔,如上所示。让我们尝试以下示例:
#include <iostream>
using namespace std;
// Base class Shape
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Base class PaintCost
class PaintCost {
public:
int getCost(int area) {
return area * 70;
}
};
// Derived class
class Rectangle: public Shape, public PaintCost {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0;
}
当编译并执行上述代码时,它会产生以下结果:
Total area: 35 Total paint cost: $2450