📜  C++ |其他C++ |问题6(1)

📅  最后修改于: 2023-12-03 14:59:47.948000             🧑  作者: Mango

C++ |其他C++ |问题6

简介

本文主要介绍了C++中的一些其他问题,包括异常处理、虚函数、智能指针和多态等。这些都是C++中常见的问题,掌握它们可以更好地理解和运用C++。

异常处理

在C++中,异常处理是一种机制,用于在程序运行过程中发现错误并处理它们。异常处理分为抛出异常和处理异常两个步骤。抛出异常是在程序遇到错误时生成异常,并将其传递给程序的某个部分进行处理。处理异常是针对抛出异常的情况,执行相应操作以处理错误。

C++中的异常处理有以下几个关键字:

  • try:用于控制异常处理代码块的开始位置。
  • catch:用于处理异常,并指定处理哪个类型的异常。
  • throw:用于在程序中抛出异常。

以下是一个C++的异常处理示例:

#include <iostream>
using namespace std;

int main() {
   try {
      int x = 5;
      int y = 0;
      if (y == 0) {
         throw "Division by zero!";
      }
      int z = x / y;
   } catch(const char* msg) {
      cerr << msg << endl;
   }
   return 0;
}

在该示例中,程序在计算z时发现y为0,就会抛出一个字符串异常,如果未对该异常进行处理,程序就会异常终止。

虚函数

在C++中,虚函数(virtual function)是一种特殊的函数,用于实现多态。虚函数是一种在基类中声明的函数,可以在派生类中进行重写。

以下是一个实现多态的C++示例:

#include <iostream>
using namespace std;

class Base {
   public:
      virtual void display() {
         cout<<"Base class"<<endl;
      }
};

class Derived1: public Base {
   public:
      void display() {
         cout<<"Derived 1"<<endl;
      }
};

class Derived2: public Base {
   public:
      void display() {
         cout<<"Derived 2"<<endl;
      }
};

int main() {
   Base *b;
   Derived1 d1;
   Derived2 d2;
   b = &d1;
   b->display();
   b = &d2;
   b->display();
   return 0;
}

在该示例中,定义了一个基类Base和两个派生类Derived1和Derived2,它们都重写了基类的虚函数display。在main函数中声明一个Base类的指针,将其分别指向两个派生类的对象,并调用display函数,程序会根据指针所指向的对象类型,自动执行相应的display函数。

智能指针

在C++中,智能指针(smart pointer)是一种用于管理动态分配的内存的指针,可以防止内存泄漏。智能指针基于RAII(Resource Acquisition Is Initialization)机制,对象在创建时分配空间,在对象生命周期结束时释放空间。

C++11中提供了三种智能指针:

  • unique_ptr:使用独占的所有权管理指针,不能进行拷贝或赋值操作。
  • shared_ptr:使用共享所有权管理指针,可以进行拷贝或赋值操作,引用计数机制。
  • weak_ptr:用于解决shared_ptr循环引用问题,不掌管对象生存期,只能由shared_ptr转化而来。

以下是一个使用智能指针的C++示例:

#include <iostream>
#include <memory>
using namespace std;

class MyClass {
   public:
      MyClass() {
         cout<<"MyClass constructed"<<endl;
      }
      ~MyClass() {
         cout<<"MyClass destructed"<<endl;
      }
      void display() {
         cout<<"Hello, world!"<<endl;
      }
};

int main() {
   unique_ptr<MyClass> uPtr(new MyClass);
   uPtr->display();

   shared_ptr<MyClass> sPtr1(new MyClass);
   shared_ptr<MyClass> sPtr2 = sPtr1;
   cout<<"sPtr1 use count: "<<sPtr1.use_count()<<endl;

   shared_ptr<MyClass> sPtr3(new MyClass);
   weak_ptr<MyClass> wPtr(sPtr3);

   return 0;
}

在该示例中,使用了unique_ptr、shared_ptr和weak_ptr。在程序运行时,会自动创建、使用和销毁对象,确保内存管理正确。

多态

在C++中,多态(polymorphism)指的是同一类型的对象在不同的情况下,表现出不同的行为。多态分为编译时多态和运行时多态两种,其中运行时多态是通过虚函数实现的,而编译时多态是通过模板实现的。

以下是一个使用多态的C++示例:

#include <iostream>
using namespace std;

class Shape {
   protected:
      int width, height;
   public:
      Shape(int w=0, int h=0) {
         width = w;
         height = h;
      }
      virtual int area() {
         cout<<"Parent class area :"<<endl;
         return 0;
      }
};

class Rectangle: public Shape{
   public:
      Rectangle(int w=0, int h=0):Shape(w, h) { }
     
      int area () {
         cout<<"Rectangle class area :"<<endl;
         return (width * height);
      }
};

class Triangle: public Shape{
   public:
      Triangle(int w=0, int h=0):Shape(w, h) { }

      int area () {
         cout<<"Triangle class area :"<<endl;
         return (width * height / 2);
      }
};

int main() {
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   shape = &rec;
   shape->area();

   shape = &tri;
   shape->area();

   return 0;
}

在该示例中,定义了一个基类Shape和两个派生类Rectangle和Triangle,它们都重写了基类的虚函数area。在main函数中声明一个Shape类的指针,依次指向两个派生类的对象,并调用area函数,程序会根据指针所指向的对象类型,自动执行相应的area函数,从而实现多态。

结论

本文介绍了C++中的一些其他问题,包括异常处理、虚函数、智能指针和多态。这些都是C++中常见的问题,掌握它们可以更好地理解和运用C++。在实际工作中,我们应该灵活应用这些问题,设计出更加健壮高效的程序。