面试题:C 和 C++ 的区别?

C 和 C++ 是两种广泛使用的编程语言,C++ 是 C 的超集,意味着 C++ 包含了 C 的大部分特性,并在此基础上增加了许多新特性。以下是 C 和 C++ 的主要区别:


1. 编程范式

  • C
    • 支持过程式编程(Procedural Programming)。
    • 主要关注函数和过程。
  • C++
    • 支持多范式编程,包括过程式编程、面向对象编程(OOP)和泛型编程。
    • 引入了类、对象、继承、多态等面向对象特性。

2. 面向对象编程(OOP)

  • C
    • 不支持面向对象编程。
    • 没有类、对象、继承、多态等概念。
  • C++
    • 支持面向对象编程。
    • 引入了类(class)、对象、继承、多态、封装等特性。
    • 示例:
      class Animal {
      public:
      virtual void makeSound() {
      std::cout << "Animal sound" << std::endl;
      }
      };
      class Dog : public Animal {
      public:
      void makeSound() override {
      std::cout << "Woof!" << std::endl;
      }
      };
      class Animal {
      public:
          virtual void makeSound() {
              std::cout << "Animal sound" << std::endl;
          }
      };
      
      class Dog : public Animal {
      public:
          void makeSound() override {
              std::cout << "Woof!" << std::endl;
          }
      };
      class Animal { public: virtual void makeSound() { std::cout << "Animal sound" << std::endl; } }; class Dog : public Animal { public: void makeSound() override { std::cout << "Woof!" << std::endl; } };

3. 标准库

  • C
    • 标准库较小,主要包括标准输入输出(stdio.h)、数学函数(math.h)、字符串处理(string.h)等。
  • C++
    • 标准库更丰富,包括:
      • STL(Standard Template Library):提供容器(如 vectormap)、算法(如 sortfind)和迭代器。
      • I/O 流:如 iostreamcincout)。
      • 字符串类std::string
    • 示例:
      #include <iostream>
      #include <vector>
      #include <algorithm>
      int main() {
      std::vector<int> vec = {3, 1, 4, 1, 5};
      std::sort(vec.begin(), vec.end());
      for (int val : vec) {
      std::cout << val << " ";
      }
      return 0;
      }
      #include <iostream>
      #include <vector>
      #include <algorithm>
      
      int main() {
          std::vector<int> vec = {3, 1, 4, 1, 5};
          std::sort(vec.begin(), vec.end());
          for (int val : vec) {
              std::cout << val << " ";
          }
          return 0;
      }
      #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {3, 1, 4, 1, 5}; std::sort(vec.begin(), vec.end()); for (int val : vec) { std::cout << val << " "; } return 0; }

4. 内存管理

  • C
    • 使用 malloccallocrealloc 和 free 进行动态内存管理。
    • 示例:
      int* arr = (int*)malloc(10 * sizeof(int));
      free(arr);
      int* arr = (int*)malloc(10 * sizeof(int));
      free(arr);
      int* arr = (int*)malloc(10 * sizeof(int)); free(arr);
  • C++
    • 使用 new 和 delete 进行动态内存管理。
    • 支持智能指针(如 std::unique_ptrstd::shared_ptr)自动管理内存。
    • 示例:
      int* arr = new int[10];
      delete[] arr;
      std::unique_ptr<int> ptr = std::make_unique<int>(10);
      int* arr = new int[10];
      delete[] arr;
      
      std::unique_ptr<int> ptr = std::make_unique<int>(10);
      int* arr = new int[10]; delete[] arr; std::unique_ptr<int> ptr = std::make_unique<int>(10);

5. 函数特性

  • C
    • 不支持函数重载(Function Overloading)。
    • 不支持默认参数。
  • C++
    • 支持函数重载。
    • 支持默认参数。
    • 示例:
      void print(int x) {
      std::cout << "Integer: " << x << std::endl;
      }
      void print(double x) {
      std::cout << "Double: " << x << std::endl;
      }
      void print(int x, int y = 10) {
      std::cout << "x: " << x << ", y: " << y << std::endl;
      }
      void print(int x) {
          std::cout << "Integer: " << x << std::endl;
      }
      
      void print(double x) {
          std::cout << "Double: " << x << std::endl;
      }
      
      void print(int x, int y = 10) {
          std::cout << "x: " << x << ", y: " << y << std::endl;
      }
      void print(int x) { std::cout << "Integer: " << x << std::endl; } void print(double x) { std::cout << "Double: " << x << std::endl; } void print(int x, int y = 10) { std::cout << "x: " << x << ", y: " << y << std::endl; }

6. 异常处理

  • C
    • 不支持异常处理。
    • 通常使用错误码或 setjmp/longjmp 处理错误。
  • C++
    • 支持异常处理(trycatchthrow)。
    • 示例:
      try {
      throw std::runtime_error("Error occurred");
      } catch (const std::exception& e) {
      std::cout << e.what() << std::endl;
      }
      try {
          throw std::runtime_error("Error occurred");
      } catch (const std::exception& e) {
          std::cout << e.what() << std::endl;
      }
      try { throw std::runtime_error("Error occurred"); } catch (const std::exception& e) { std::cout << e.what() << std::endl; }

7. 模板和泛型编程

  • C
    • 不支持模板和泛型编程。
  • C++
    • 支持模板(template),实现泛型编程。
    • 示例:
      template <typename T>
      T add(T a, T b) {
      return a + b;
      }
      int main() {
      std::cout << add(3, 4) << std::endl; // 输出 7
      std::cout << add(3.5, 4.5) << std::endl; // 输出 8.0
      return 0;
      }
      template <typename T>
      T add(T a, T b) {
          return a + b;
      }
      
      int main() {
          std::cout << add(3, 4) << std::endl; // 输出 7
          std::cout << add(3.5, 4.5) << std::endl; // 输出 8.0
          return 0;
      }
      template <typename T> T add(T a, T b) { return a + b; } int main() { std::cout << add(3, 4) << std::endl; // 输出 7 std::cout << add(3.5, 4.5) << std::endl; // 输出 8.0 return 0; }

8. 命名空间

  • C
    • 不支持命名空间。
  • C++
    • 支持命名空间(namespace),用于避免命名冲突。
    • 示例:
      namespace MyNamespace {
      int x = 10;
      }
      int main() {
      std::cout << MyNamespace::x << std::endl; // 输出 10
      return 0;
      }
      namespace MyNamespace {
          int x = 10;
      }
      
      int main() {
          std::cout << MyNamespace::x << std::endl; // 输出 10
          return 0;
      }
      namespace MyNamespace { int x = 10; } int main() { std::cout << MyNamespace::x << std::endl; // 输出 10 return 0; }

9. 引用

  • C
    • 不支持引用。
  • C++
    • 支持引用(&),提供更安全的指针替代。
    • 示例:
      int x = 10;
      int& ref = x;
      ref = 20;
      std::cout << x << std::endl; // 输出 20
      int x = 10;
      int& ref = x;
      ref = 20;
      std::cout << x << std::endl; // 输出 20
      int x = 10; int& ref = x; ref = 20; std::cout << x << std::endl; // 输出 20

10. 总结

特性CC++
编程范式过程式编程多范式编程(过程式、面向对象、泛型)
面向对象不支持支持
标准库较小更丰富(STL、I/O 流、字符串类等)
内存管理mallocfreenewdelete,智能指针
函数特性不支持重载、默认参数支持重载、默认参数
异常处理不支持支持
模板不支持支持
命名空间不支持支持
引用不支持支持

C++ 在 C 的基础上增加了许多新特性,更适合现代软件开发,但 C 仍然在系统编程和嵌入式开发中占据重要地位。选择使用 C 还是 C++ 取决于具体的应用场景和需求。

THE END
点赞8 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容