目录
摘要
封装(Encapsulation)
基础用法
继承(Inheritance)
基础用法
多态(Polymorphism)
1. 虚函数
2. 纯虚函数
总结
摘要
在C++中,封装、继承和多态是面向对象编程的三大核心特性。为了方便大家理解,下面我将举一些例子来简单说明一下。
封装(Encapsulation)
封装是将数据和操作数据的方法捆绑在一起,并隐藏对象的内部实现细节,只暴露必要的接口给外部使用。C++通过类和访问修饰符(public、protected、private)来实现封装。
基础用法
#include <iostream>
#include <string>class Food {
private:std::string name; // 食品名称double calories; // 卡路里含量public:// 构造函数Food(const std::string& foodName, double foodCalories): name(foodName), calories(foodCalories) {}// 设置食品名称的方法void setName(const std::string& foodName) {name = foodName;}// 获取食品名称的方法std::string getName() const {return name;}// 设置卡路里含量的方法void setCalories(double foodCalories) {calories = foodCalories;}// 获取卡路里含量的方法double getCalories() const {return calories;}
};int main() {Food apple("Apple", 52); // 创建食品对象std::cout << "Food: " << apple.getName() << ", Calories: " << apple.getCalories() << std::endl;apple.setName("Green Apple");apple.setCalories(48);std::cout << "Food: " << apple.getName() << ", Calories: " << apple.getCalories() << std::endl;return 0;
}
继承(Inheritance)
继承允许一个类(子类)从另一个类(父类)继承数据成员和成员函数,从而实现代码复用。C++支持单继承和多继承。
基础用法
#include <iostream>
#include <string>// 基类 Food
class Food {
protected:std::string name;double calories;public:// 构造函数Food(const std::string& foodName, double foodCalories): name(foodName), calories(foodCalories) {}// 获取食品名称的方法std::string getName() const {return name;}// 获取卡路里含量的方法double getCalories() const {return calories;}
};// 派生类 Fruit 继承自 Food
class Fruit : public Food {
public:// 构造函数Fruit(const std::string& fruitName, double fruitCalories): Food(fruitName, fruitCalories) {}// 显示水果信息的方法void displayInfo() const {std::cout << "Fruit: " << name << ", Calories: " << calories << " kcal" << std::endl;}
};// 派生类 Vegetable 继承自 Food
class Vegetable : public Food {
public:// 构造函数Vegetable(const std::string& vegetableName, double vegetableCalories): Food(vegetableName, vegetableCalories) {}// 显示蔬菜信息的方法void displayInfo() const {std::cout << "Vegetable: " << name << ", Calories: " << calories << " kcal" << std::endl;}
};int main() {Fruit apple("Apple", 52); // 创建水果对象Vegetable carrot("Carrot", 41); // 创建蔬菜对象apple.displayInfo(); // 显示水果信息carrot.displayInfo(); // 显示蔬菜信息return 0;
}
多态(Polymorphism)
多态允许使用基类的指针或引用来调用子类的重写方法,实际调用的方法在运行时决定。C++中的多态性通过虚函数实现。
1. 虚函数
#include <iostream>
#include <string>// 基类 Food
class Food {
public:// 虚函数 displayInfovirtual void displayInfo() const {std::cout << "This is a food item." << std::endl;}
};// 派生类 Fruit 继承自 Food
class Fruit : public Food {
private:std::string name;double calories;public:// 构造函数Fruit(const std::string& fruitName, double fruitCalories): name(fruitName), calories(fruitCalories) {}// 重写 displayInfo 方法void displayInfo() const override {std::cout << "Fruit: " << name << ", Calories: " << calories << " kcal" << std::endl;}
};// 派生类 Vegetable 继承自 Food
class Vegetable : public Food {
private:std::string name;double calories;public:// 构造函数Vegetable(const std::string& vegetableName, double vegetableCalories): name(vegetableName), calories(vegetableCalories) {}// 重写 displayInfo 方法void displayInfo() const override {std::cout << "Vegetable: " << name << ", Calories: " << calories << " kcal" << std::endl;}
};int main() {Food* food;Fruit apple("Apple", 52);Vegetable carrot("Carrot", 41);food = &apple;food->displayInfo(); // 将调用 Fruit 的 displayInfo 方法food = &carrot;food->displayInfo(); // 将调用 Vegetable 的 displayInfo 方法return 0;
}
2. 纯虚函数
#include <iostream>
#include <string>// 抽象基类 Drink
class Drink {
public:// 纯虚函数 displayInfovirtual void displayInfo() const = 0; // 定义接口,但没有实现
};// 派生类 Juice 继承自 Drink
class Juice : public Drink {
private:std::string name;double volume;public:// 构造函数Juice(const std::string& juiceName, double juiceVolume): name(juiceName), volume(juiceVolume) {}// 实现纯虚函数 displayInfovoid displayInfo() const override {std::cout << "Juice: " << name << ", Volume: " << volume << " ml" << std::endl;}
};// 派生类 Soda 继承自 Drink
class Soda : public Drink {
private:std::string name;double volume;public:// 构造函数Soda(const std::string& sodaName, double sodaVolume): name(sodaName), volume(sodaVolume) {}// 实现纯虚函数 displayInfovoid displayInfo() const override {std::cout << "Soda: " << name << ", Volume: " << volume << " ml" << std::endl;}
};int main() {Juice orangeJuice("Orange Juice", 250);Soda cola("Cola", 330);// 创建 Drink 类型的指针数组Drink* drinks[2];drinks[0] = &orangeJuice;drinks[1] = &cola;// 遍历数组并调用 displayInfo 方法for (int i = 0; i < 2; ++i) {drinks[i]->displayInfo(); // 将调用具体对象的 displayInfo 方法}return 0;
}
总结
1. 封装:通过类和访问控制符(public、protected、private)实现数据隐藏和接口统一。
2. 继承:允许一个类继承另一个类的属性和方法,促进代码复用。
3. 多态:允许基类指针或引用调用子类的重写方法,实际调用的方法在运行时确定。