封装、继承和多态
1. 封装
1.1 理论解释
封装是面向对象编程(OOP)中的一个基本概念,它指的是将数据和操作数据的方法绑定在一起,并隐藏对象的内部状态,只通过公共接口与外部进行交互。封装的主要目的是保护数据的完整性和安全性,防止外部代码直接访问和修改对象的内部状态。
1.2 代码示例
#include <iostream>
using namespace std;
class Box {
private:
double length;
double width;
double height;
public:
// 构造函数
Box(double l, double w, double h) : length(l), width(w), height(h) {}
// 公共接口
double getVolume() {
return length * width * height;
}
};
int main() {
Box box(10.0, 20.0, 30.0);
cout << "Volume of the box is: " << box.getVolume() << endl;
return 0;
}
1.3 实践练习
- 创建一个名为
Circle的类,包含私有成员radius。 - 提供一个公共接口
getArea()来计算并返回圆的面积。 - 在
main函数中创建一个Circle对象并输出其面积。
2. 继承
2.1 理论解释
继承是面向对象编程中的另一个重要概念,它允许一个类(子类或派生类)继承另一个类(父类或基类)的属性和方法。通过继承,子类可以重用父类的代码,并且可以添加新的属性和方法,或者重写父类的方法。
2.2 代码示例
#include <iostream>
using namespace std;
class Shape {
protected:
double width;
double height;
public:
Shape(double w, double h) : width(w), height(h) {}
virtual double getArea() {
return 0;
}
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : Shape(w, h) {}
double getArea() override {
return width * height;
}
};
int main() {
Rectangle rect(10.0, 20.0);
cout << "Area of the rectangle is: " << rect.getArea() << endl;
return 0;
}
2.3 实践练习
- 创建一个名为
Animal的基类,包含一个虚函数makeSound()。 - 创建两个派生类
Dog和Cat,分别重写makeSound()方法。 - 在
main函数中创建Dog和Cat对象,并调用makeSound()方法。
3. 多态
3.1 理论解释
多态是面向对象编程中的第三个重要概念,它允许使用基类的指针或引用来调用派生类的对象。多态性使得代码更加灵活和可扩展,因为可以在不修改现有代码的情况下添加新的派生类。
3.2 代码示例
#include <iostream>
using namespace std;
class Shape {
protected:
double width;
double height;
public:
Shape(double w, double h) : width(w), height(h) {}
virtual double getArea() {
return 0;
}
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : Shape(w, h) {}
double getArea() override {
return width * height;
}
};
class Triangle : public Shape {
public:
Triangle(double w, double h) : Shape(w, h) {}
double getArea() override {
return 0.5 * width * height;
}
};
int main() {
Shape* shape;
Rectangle rect(10.0, 20.0);
Triangle tri(10.0, 20.0);
shape = ▭
cout << "Area of the rectangle is: " << shape->getArea() << endl;
shape = &tri;
cout << "Area of the triangle is: " << shape->getArea() << endl;
return 0;
}
3.3 实践练习
- 创建一个名为
Vehicle的基类,包含一个虚函数drive()。 - 创建两个派生类
Car和Bike,分别重写drive()方法。 - 在
main函数中创建Vehicle指针,并分别指向Car和Bike对象,调用drive()方法。
4. 总结
封装、继承和多态是面向对象编程的三大核心概念。封装通过隐藏对象的内部状态来保护数据;继承通过重用和扩展基类的代码来提高代码的复用性;多态通过基类的指针或引用来调用派生类的对象,使得代码更加灵活和可扩展。掌握这些概念对于编写高质量的面向对象程序至关重要。
5. 下一步
在掌握了封装、继承和多态的基本概念后,你可以继续学习构造函数和析构函数、虚函数和抽象类等更高级的面向对象编程技术。这些技术将进一步增强你对C++的理解和应用能力。